Like many programs, the first step is to carefully
describe the process in English before trying to code it in
Java. If you're not clear on the steps that are required, there's little chance of the code working.
In this case, I suggest you
start small and gradually expand your program. For example, write a program that covers cases 1-10 first. When that works, expand it to cover 1-20. When that works, expand it to cover 1-50. Etc. This should help you see the
patterns for extending it to cover all cases.