Subarray Up To K
Problem Statement
Given an array a of n integers a1,a2,...,an, as well as an integer k, determine the sum of the subarray which has a sum which is less than or equal to k. There will always be a subarray with a sum of 0. If there is no subarray with sum less than or equal to k then output −1.
Remember that a subarray is a selection of continuous elements of a.
Input Format
Your first line will contain two space-separated integers n and k. Your next line will contain n space-separated integers a1,...,an.
Output Format
You should output the largest subarray sum less than or equal to k. If there is no subarray with sum less than or equal to k then output −1.
Constraints
- 1≤n≤105
- −109≤ai≤109
- −109≤k≤109
Sample Cases
6 13
100 -4 2 7 2 5
12
Explanation 2
The subarray with sum of 12 is [−4,2,7,2,5].
5 5
1 2 -4 2 5
5
Explanation 2
The subarray with sum of 5 is [5].
Comments0
No comments yet
Be the first to comment.
New comment
Log in to join the discussion.