Java implementation of the above approach
import java . util . ArrayList ; import java . util . Arrays ; import java . util . Collections ; import java . util . Comparator ; import java . util . HashMap ; import java . util . Map ; class GFG { static class Pair < K , V > { K first ; V second ; public Pair ( K first , V second ) { this . first = first ; this . second = second ; } public static < K , V > Pair < K , V > of ( K first , V second ) { return new Pair < > ( first , second ) ; } }