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
def countTriplets ( arr , N ) : NEW_LINE INDENT zero_i = [ ] NEW_LINE one_i = [ ] NEW_LINE mp = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] == 0 ) : NEW_LINE INDENT zero_i . append ( i + 1 ) NEW_LINE DEDENT elif ( arr [ i ] == 1 ) : NEW_LINE INDENT one_i . append ( i + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT mp [ i + 1 ] = 1 NEW_LINE DEDENT DEDENT total = len ( zero_i ) * len ( one_i ) * len ( mp ) NEW_LINE for i in range ( len ( zero_i ) ) : NEW_LINE INDENT for j in range ( len ( one_i ) ) : NEW_LINE INDENT p = zero_i [ i ] NEW_LINE q = one_i [ j ] NEW_LINE r = 2 * p - q NEW_LINE if ( r in mp ) : NEW_LINE INDENT total -= 1 NEW_LINE DEDENT r = 2 * q - p NEW_LINE if ( r in mp ) : NEW_LINE INDENT total -= 1 NEW_LINE DEDENT r = ( p + q ) // 2 NEW_LINE if ( ( r in mp ) and abs ( r - p ) == abs ( r - q ) ) : NEW_LINE INDENT total -= 1 NEW_LINE DEDENT DEDENT DEDENT print ( total ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 , 1 , 2 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE countTriplets ( arr , N ) NEW_LINE DEDENT