Problem in logic of code

Hello, Please explain me the logic of this problem? How many variables should I take? How should I handle this problem ? Explain me its pseudocode.