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

Method minSubArrayLen

SubarraySumGreaterThanX.java:37–59  ·  view source on GitHub ↗
(int target, int[] nums)

Source from the content-addressed store, hash-verified

35
36class Solution {
37 public int minSubArrayLen(int target, int[] nums) {
38 int start=0,end=1;
39 int sum=nums[start];
40 if(sum >= target) return 1;
41 int n = nums.length;
42 int result = Integer.MAX_VALUE;
43 if(end<nums.length) sum+=nums[end];
44 while(start<n && end<n)
45 {
46 if(sum>=target)
47 {
48 result=Math.min(result,end-start+1);
49 sum-=nums[start];
50 start++;
51 }
52 else
53 {
54 end++;
55 if(end<n) sum+=nums[end];
56 }
57 }
58 return (result == Integer.MAX_VALUE)?0:result;
59 }
60}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected