Very Simple java Solution


  • 0
    N
    public class Solution {
        public int numberOfArithmeticSlices(int[] A) {
            int count = 0;
            int len = A.length;
            if(len  < 3) {
                return 0;
            }
            for (int l = 3; l <= len; l++) {
                for(int i = 0; i <len-l+1; i++) {
                    int j = i+1;
                    int dist = A[j] - A[i];
                    boolean flag = false;
                    while(j < i+l) {
                        if((A[j]  - A[j-1]) != dist) {
                            flag = true;
                            break;
                        }
                        j++;
                    }
                    if(!flag) {
                        count++;
                    }
                }
                System.out.println(l+" - "+count);
            }
            return count;
        }
    }
    
    `

  • 0
    N

    This program is just like matrix chain multiplication. Here 'l' represents how long chain we choose and using 'i' we mention start and using 'j' we make sure that the difference between the consecutive elements in that window is same.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.