Java implementation of the above approach
import java . util . Arrays ; class Pair implements Comparable < Pair > { int ind , val ; Pair ( int ind , int val ) { this . ind = ind ; this . val = val ; } @ Override public int compareTo ( Pair o ) { if ( this . val > o . val ) return 1 ; return - 1 ; } } class GFG {