Count pairs in an array such that frequency of one is at least value of other | Java program to find number of ordered pairs ; Function to find count of Ordered pairs ; Initialize pairs to 0 ; Store frequencies ; Count total Ordered_pairs ; Driver Code
import java . util . HashMap ; import java . util . Map ; class GFG { public static int countOrderedPairs ( int [ ] A , int n ) { int orderedPairs = 0 ; HashMap < Integer , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . get ( A [ i ] ) == null ) m . put ( A [ i ] , 1 ) ; else { int a = m . get ( A [ i ] ) ; m . put ( A [ i ] , ++ a ) ; } } for ( int entry : m . keySet ( ) ) { int X = entry ; int Y = m . get ( entry ) ; for ( int j = 1 ; j <= Y ; j ++ ) { if ( m . get ( j ) >= X ) orderedPairs ++ ; } } return orderedPairs ; } public static void main ( String [ ] args ) { int [ ] A = { 1 , 1 , 2 , 2 , 3 } ; int n = A . length ; System . out . print ( countOrderedPairs ( A , n ) ) ; } }