Quote:
How we can solve this puzzle using java ?
Not 'we', just 'you', this puzzle is a kind of contest/challenge. The goal is
you find a smart way to solve the puzzle as fast as possible.
What I can say is that brute force is never the wanted solution.
My advice:
1) Create a brute force program. Once it works, add code to count the number of operations it need adds, multiplies to get the result.
2) Do runs with dataset of 1, then 2 and so on. Note how counts evolve.
3) Think about ways to reduce those counts with smarter code.
Said otherwise, you have to create algorithm or combine known ones.
Quote:
I am not able to find the cost part of the program .
you have a detailed example, with the debugger, it is easy to compare what your code is doing against what it should.
There is a tool that allow you to see what your code is doing, its name is
debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is
debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[
^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[
^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[
^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.