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 ; Given array ; Function Call
function calcSubarray ( A , N ) { let count = 0 ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = i + 1 ; j < N ; j ++ ) { let flag = true ; let comm_diff = A [ i + 1 ] - A [ i ] ; for ( let k = i ; k < j ; k ++ ) { if ( A [ k + 1 ] - A [ k ] == comm_diff ) { continue ; } else { flag = false ; break ; } } if ( flag ) { count ++ ; } } } return count ; } let A = [ 8 , 7 , 4 , 1 , 0 ] ; let N = A . length ; document . write ( calcSubarray ( A , N ) ) ;