Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 6420 |

@blackshadows | 6380 |

@mb1973 | 5704 |

@Quandray | 5245 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 5046 |

@sanjay05 | 3762 |

@kirtidee18 | 3673 |

@mantu_singh | 3530 |

@marius_valentin_dragoi | 3523 |

@sushant_a | 3459 |

Complete Leaderboard | |

Handle | Score |

@cfwong8 | 1160 |

@gurshehzadsingh | 847 |

@nithinreddy3210 | 739 |

@mohanreddy8847 | 702 |

@AkashLahoty | 606 |

@jha8768 | 583 |

@workit | 582 |

@balwanyadav34 | 582 |

@pritsahkar2000 | 580 |

@dipeshtiwari47 | 575 |

@divyagupta221100 | 557 |

Complete Leaderboard |

Given an array **arr[]** of integers of size **N** and a number **X**, the task is to find the sum of subarray having maximum sum less than or equal to the given value of **X**.

**Example 1:**

**Input: **N = 5, X = 11
arr[] = {1, 2, 3, 4, 5}
**Output:** 10
**Explanation**: Subarray having maximum
sum is {1, 2, 3, 4}.

**Example 2:**

**Input: **N = 5, X = 7
arr[] = {2, 4, 6, 8, 10}
**Output:** 6
**Explanation:** Subarray having maximum
sum is {2, 4} or {6}.

**Your Task:**

This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function **calculateMaxSumLength**() that takes array **arr, **integer **N**, and integer** X **as parameters and returns maximum sum of any subarray that is less than or equal to x.

**Expected Time Complexity:** O(N).

**Expected Auxiliary Space:** O(1).

**Constraints:**

1 ≤ N ≤ 10^{6}

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Maximum sum of subarray less than or equal to x

...