Amazon Interview Question

graph coloring math puzzle next smaller element