We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
//Keeps track of possible solutions
int count = 0;
//Iterators that represend beginning and end of window.
auto winBeg = s.begin(), winEnd = s.begin();
//move end to end of window range
for(int i = 1;(i < m) && (winEnd != s.end());i++)
{
++winEnd;
}
//While the end of the window doesn't equal the end iterator of the vector
while(winEnd != s.end())
{
int sum = 0;
//Loop through window range and sum
for(auto i = winBeg; i != winEnd + 1;++i)
{
sum += *i;
}
//if sum is valid solution increment counter and move window
if(sum == d)
{
count++;
}
//else move window over by one
++winBeg;
++winEnd;
}
return count;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Subarray Division 1
You are viewing a single comment's thread. Return to all comments →
C++ 11 SOLUTION USING SLIDING WINDOW:
int birthday(vector s, int d, int m) {
}