MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / sb

Method sb

SubarraySumGreaterThanX.java:4–31  ·  view source on GitHub ↗
(int a[], int n, int x)

Source from the content-addressed store, hash-verified

2class Solution {
3
4 public static int sb(int a[], int n, int x) {
5 // Your code goes here
6
7 int start=0,end=1;
8 int result=Integer.MAX_VALUE;
9 int sum=a[start];
10 if(sum>x) return 1;
11 if(end<n) sum+=a[end];
12
13 while(start<n && end<n)
14 {
15 if(sum>x)
16 {
17 result = Math.min(result,end-start+1);
18 sum-=a[start];
19 start++;
20 }
21 else
22 {
23 end++;
24 if(end<n)
25 {
26 sum+=a[end];
27 }
28 }
29 }
30 return result;
31 }
32}
33
34//Leetcode solution

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected