Subarray Division 1

  • + 0 comments

    java solution

        public static int birthday(List<Integer> s, int d, int m) {
        // Write your code here
        //initialize the variable count to keep track of # of possible ways to divide the chocolate
        int count = 0;
        //number of ways to take m adjacent numbers in size s chocolate
        int combinations = s.size()-m+1;
        //for each possible combination
        for (int i=0; i< combinations; i++){
            int sum =0;
            for(int j=0; j<m; j++){
                //sum together m adjacent squares
                sum+=s.get(i+j);
            }
            //increment count if sum equals to birthday d
            if (sum==d){
                count++;
            }
        }
        return count;
        }