Please help how solve this question

i checked solution, but did’nt get a logic.

Tried Sc.nextLine() still not working