Infosys Interview Question

Java coding question (easy to medium)