Count non | Function to find the total count of triplets ( i , j , k ) such that i < j < k and ( j - i ) != ( k - j ) ; Stores indices of 0 s ; Stores indices of 1 s ; Stores indices of 2 s ; Traverse the array ; If current array element is 0 ; If current array element is 1 ; If current array element is 2 ; Total count of triplets ; Traverse the array zero_i [ ] ; Traverse the array one_i [ ] ; Stores index of 0 s ; Stores index of 1 s ; Stores third element of triplets that does not satisfy the condition ; If r present in the map ; Update r ; If r present in the map ; Update r ; If r present in the map and equidistant ; Print the obtained count ; Driver Code
function countTriplets ( arr , N ) { var zero_i = [ ] ; var one_i = [ ] ; var mp = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) zero_i . push ( i + 1 ) ; else if ( arr [ i ] == 1 ) one_i . push ( i + 1 ) ; else mp . set ( i + 1 , 1 ) ; } var total = zero_i . length * one_i . length * mp . size ; for ( var i = 0 ; i < zero_i . length ; i ++ ) { for ( var j = 0 ; j < one_i . length ; j ++ ) { var p = zero_i [ i ] ; var q = one_i [ j ] ; var r = 2 * p - q ; if ( mp . has ( r ) ) total -- ; r = 2 * q - p ; if ( mp . has ( r ) ) total -- ; r = ( p + q ) / 2 ; if ( mp . has ( r ) && Math . abs ( r - p ) == Math . abs ( r - q ) ) total -- ; } } document . write ( total ) ; } var arr = [ 0 , 1 , 2 , 1 ] ; var N = arr . length ; countTriplets ( arr , N ) ;