Java implementation of the above approach
import java . util . ArrayList ; import java . util . Arrays ; class GFG { static class Pair < K , V > { K first ; V second ; public Pair ( K first , V second ) { this . first = first ; this . second = second ; } } static int inf = 100000000 ;