Java implementation of the approach
import java . util . * ; class GFG { static class Pair implements Comparable < Pair > { int first , second ; Pair ( int f , int s ) { first = f ; second = s ; } @ Override