Count quadruples ( i , j , k , l ) in an array such that i < j < k < l and arr [ i ] = arr [ k ] and arr [ j ] = arr [ l ] | Function to count total number of required tuples ; Initialize unordered map ; Find the pairs ( j , l ) such that arr [ j ] = arr [ l ] and j < l ; Elements are equal ; Update the count ; Add the frequency of arr [ l ] to val ; Update the frequency of element arr [ j ] ; Return the answer ; Driver code ; Given array arr [ ] ; Function call
def countTuples ( arr , N ) : NEW_LINE INDENT ans = 0 NEW_LINE val = 0 NEW_LINE freq = { } NEW_LINE for j in range ( N - 2 ) : NEW_LINE INDENT val = 0 NEW_LINE for l in range ( j + 1 , N ) : NEW_LINE INDENT if ( arr [ j ] == arr [ l ] ) : NEW_LINE INDENT ans += val NEW_LINE DEDENT if arr [ l ] in freq : NEW_LINE INDENT val += freq [ arr [ l ] ] NEW_LINE DEDENT DEDENT freq [ arr [ j ] ] = freq . get ( arr [ j ] , 0 ) + 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 1 , 2 , 2 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countTuples ( arr , N ) ) NEW_LINE DEDENT