Count of pairs ( x , y ) in an array such that x < y | C ++ implementation of the approach ; Function to return the number of pairs ( x , y ) such that x < y ; To store the number of valid pairs ; If a valid pair is found ; Return the count of valid pairs ; Driver code
#include <iostream> NEW_LINE using namespace std ; int getPairs ( int a [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( a [ i ] < a [ j ] ) count ++ ; } } return count ; } int main ( ) { int a [ ] = { 2 , 4 , 3 , 1 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << getPairs ( a , n ) ; return 0 ; }