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 ] | Java program for the above approach ; 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
import java . util . * ; class GFG { static int countTuples ( int arr [ ] , int N ) { int ans = 0 , val = 0 ; HashMap < Integer , Integer > freq = new HashMap < Integer , Integer > ( ) ; for ( int j = 0 ; j < N - 2 ; j ++ ) { val = 0 ; for ( int l = j + 1 ; l < N ; l ++ ) { if ( arr [ j ] == arr [ l ] ) { ans += val ; } if ( freq . containsKey ( arr [ l ] ) ) val += freq . get ( arr [ l ] ) ; } if ( freq . containsKey ( arr [ j ] ) ) { freq . put ( arr [ j ] , freq . get ( arr [ j ] ) + 1 ) ; } else { freq . put ( arr [ j ] , 1 ) ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 1 , 2 , 2 , 2 } ; int N = arr . length ; System . out . print ( countTuples ( arr , N ) ) ; } }