Example Problem How do I take the input of this type in Java where n is not given?
Check some JAVA solutions of this problem, it should help. CodeChef: Practical coding for everyone