Java program for the above approach
import java . util . * ; class GFG { static class pair implements Comparable < pair > { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } public int compareTo ( pair p ) { return this . first - p . first ; } }