Tuesday, December 11, 2018

[Devskill] DCP-98: How Lazy One can be Part 2

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : DCP-98: How Lazy One can be Part 2
Source            : Devskill
Category          : Data Structure
Algorithm         : Two Pointer
Verdict           : Accepted
#include <bits/stdc++.h>
 
using namespace std;
 
int main()
{
      int tc;
      scanf("%d", &tc); 
      for (int tcase=1; tcase<=tc; tcase++)
      {
            int n, s;
            scanf("%d %d", &n, &s);
            vector <int> vec;
            for (int i=0; i<n; i++)
            {
                  int num;
                  scanf("%d", &num);
                  vec.push_back(num);
            }
            int minLen = n+1;
            int sum = 0;
            int low = 0;
            int high = 0;
            while (high <= n)
            {
                  if (sum >= s)
                  {
                        minLen = min(minLen, high-low);
                  }
                  if (sum >= s && low < high)
                  {
                        sum -= vec[low];
                        low++;
                  }
                  else
                  {
                        sum += vec[high];
                        high++;
                  }
             }
             if (minLen == n+1) minLen = -1;
             printf("Case %d: %d\n", tcase, minLen);
             vec.clear();
      }
      return 0;
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.