Java program of the above approach
import java . io . * ; import java . util . * ; class Pair { int first , second ; Pair ( int first , int second ) { this . first = first ; this . second = second ; } } class GFG {