Count of subarrays forming an Arithmetic Progression ( AP ) | Function to find the total count of subarrays ; Iterate over each subarray ; Difference between first two terms of subarray ; Iterate over the subarray from i to j ; Check if the difference of all adjacent elements is same ; Driver Code ; Given array ; Function call
def calcSubarray ( A , N ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT flag = True NEW_LINE comm_diff = A [ i + 1 ] - A [ i ] NEW_LINE for k in range ( i , j ) : NEW_LINE INDENT if ( A [ k + 1 ] - A [ k ] == comm_diff ) : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT flag = False NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 8 , 7 , 4 , 1 , 0 ] NEW_LINE N = len ( A ) NEW_LINE print ( calcSubarray ( A , N ) ) NEW_LINE DEDENT