How to solve this?

how to solve this question?

Hi @akshitabansal321,
Refer

in this solution they are using graph

@akshitabansal321, https://www.youtube.com/watch?v=m-8_yQao-lI&t=62s