Sreekar_0125

Untitled

Aug 25th, 2023
827
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. int minSubArrayLen(int target, vector<int>& nums) {
  2.         int n = nums.size();
  3.         for(int i = 0; i < n; i++)
  4.         {
  5.             if(nums[i] >= target) return 1;
  6.         }
  7.         for(int i = 1; i < n; i++)
  8.         {
  9.             nums[i] += nums[i - 1];
  10.         }
  11.         int low = 0;
  12.         int high = 1;
  13.  
  14.         int minLength = (int)1e9;
  15.         while(high < n)
  16.         {
  17.             int sum = ((nums[high]) - (low == 0 ? 0 : nums[low - 1]));
  18.             if(sum < target)
  19.             {
  20.                 high++;
  21.             }
  22.             else
  23.             {
  24.                 minLength = min(minLength, (high - low + 1));
  25.                 low++;
  26.             }
  27.         }
  28.         // 2 5 6 8 12 15
  29.         return minLength == 1e9 ? 0 : minLength;
  30.     }
Advertisement
Add Comment
Please, Sign In to add comment