Advertisement
LoopyMango

hehe2

Jun 1st, 2023
559
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. int smallestSubWithSum(int arr[], int n, int x)
  2.     {
  3.         // Your code goes here  
  4.         int k=0;
  5.         int kk=0;
  6.         int sum=0;
  7.         int ans  = INT_MAX;
  8.          while(true)
  9.          {
  10.              if(sum > x)
  11.              { if (k >n-1)
  12.                  break;
  13.                  sum-=arr[k];
  14.                  k++;
  15.                  ans= min(ans,kk-k+1);
  16.                
  17.              }
  18.               else
  19.               { if(kk>n-1)
  20.                   break;
  21.                   sum+=arr[kk];
  22.                   kk++;
  23.                  
  24.               }
  25.              
  26.              
  27.          }
  28.        
  29.        
  30.         if(ans == INT_MAX)
  31.          return 0;
  32.         return ans;
  33.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement