Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int atMostSum(List<Integer> a,
- int k)
- {
- int sum = 0;
- int cnt = 0, maxcnt = 0;
- for (int i = 0; i < a.size(); i++) {
- // If adding current element doesn't
- // cross limit add it to current window
- if ((sum + a.get(i)) <= k) {
- sum += a.get(i);
- cnt++;
- }
- // Else, remove first element of current
- // window.
- else if(sum!=0)
- {
- sum = sum - a.get(i - cnt) + a.get(i);
- }
- // keep track of max length.
- maxcnt = Math.max(cnt, maxcnt);
- }
- return maxcnt;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement