text: Check if an array of pairs can be sorted by swapping pairs with different first elements | Function to check if an array is sorted or not ; Traverse the array arr [ ] ; Return true ; Function to check if it is possible to sort the array w . r . t . first element ; Stores the ID of the first element ; Traverse the array arr [ ] ; If arr [ i ] . second is not equal to that of the group ; If array is sorted ; Driver Code
code: function isSorted ( arr , N ) { for ( let i = 1 ; i < N ; i ++ ) { if ( arr [ i ] [ 0 ] > arr [ i - 1 ] [ 0 ] ) { return false ; } } return true ; } function isPossibleToSort ( arr , N ) { let group = arr [ 0 ] [ 1 ] ; for ( let i = 1 ; i < N ; i ++ ) { if ( arr [ i ] [ 1 ] != group ) { return " " ; } } if ( isSorted ( arr , N ) ) { return " " ; } else { return " " ; } } let arr = [ [ 340000 , 2 ] , [ 15000 , 2 ] , [ 34000 , 2 ] , [ 10000 , 2 ] ] ; let N = arr . length ; document . write ( isPossibleToSort ( arr , N ) ) ;