• + 9 comments

    Here is the Java implemetation. We just need the starting index that we are working on. and every time there is an ArrayIndexOutofBoundsException that means we don't need the loop anymore and we break. We count the total of consecutive squares and check if it's equal to the d.

    static int solve(int n, int[] s, int d, int m){
            int startingIndex =0;
            int result =0;
            int total = 0;
            
            for(int i=0;i<n;i++){
                startingIndex = i;
                
                try{
                for(int j=0;j<m;j++){
                    total += s[startingIndex + j];
                }   
                }catch(Exception e){
                    break;
                }
                
                if(total == d){
                    result++;
                }
                total =0;
            }
            return result;
        }