Problem Description

Given an array of integers and a target sum, find the maximum possible sum of a contiguous subarray such that the sum does not exceed the target value. If multiple subarrays have the same maximum sum, return the one with the smallest length. If no such subarray exists, return 0.

Input Format

- First line:
Two integers n and target (1 ≤ n ≤ 10^5, -10^9 ≤ target ≤ 10^9)
- Second line:
n space-separated integers (-10^4 ≤ arr[i] ≤ 10^4)

Output Format

- Single integer representing the maximum subarray sum that does not exceed target
- If no valid subarray exists, print 0

Sample Test Case

Input:
``` 8 7 1 -3 4 2 -1 2 1 -5 ```
Output:
``` 6 ```

Constraints

- Time complexity: O(n log n) or better
- Space complexity: O(n) or better
- Handle edge cases like all negative numbers
- Handle cases where target is negative

Solutions (0)

No solutions submitted yet. Be the first!

Discussion (0)

No comments yet. Start the discussion!

Prev Next