• + 0 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:

    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;       
    }