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.
Is it me or this is not the "same approach"? Correct me if i'm wrong but i think Arrays.stream.sum() iterates fron index a to b, so in every iteration of the for loop you are iterating again. Your algorithm is not 0(n) as @Saikumar_P's. This is my 0(n) solution with Java similar to @Saiumar_P's:
static int birthday(List s, int d, int m) {
int[] sums = new int[s.size() + 1];
int count = 0;
for(int i = 0; i < s.size(); i++) {
int sumsIndex = i + 1;
sums[sumsIndex] = sums[sumsIndex - 1] + s.get(i);
if (sumsIndex >= m && (sums[sumsIndex] - sums[sumsIndex - m]) == d)
count++;
}
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
You are viewing a single comment's thread. Return to all comments →
Is it me or this is not the "same approach"? Correct me if i'm wrong but i think Arrays.stream.sum() iterates fron index a to b, so in every iteration of the for loop you are iterating again. Your algorithm is not 0(n) as @Saikumar_P's. This is my 0(n) solution with Java similar to @Saiumar_P's: