C++ 7 lines code, O(n) time complexity, no DP.


  • 0
    A
    int i=0, count = 0;
    for(int j = 1;j<A.size();j++)
    	if(j+1>=A.size() || A[j]-A[j-1] != A[j+1]-A[j]){
    		count+=(j-i)*(j-i-1)/2;
    		i=j;
    	}
    return count;

Log in to reply
 

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