For most puzzles, if we can understand what the question says, we will be able to give a direct answer. But that simple answer is always not so good in terms of efficiency.Sometimes this is because we do a lot of duplicate computation.
Try this. Have a look at the basic operations of your algorithm performed by seemingly independent part, especially those performing on the same data set. Then try to identify what basic operations can be reused, record the result of the basic operation at some place reuse it later.
Exampe question:
TADM.3-29
You have an unordered array X of n integers. Find the array M containing
n elements where Mi is the product of all integers in X except for Xi. You may
not use division. You can use extra memory. (Hint: There are solutions faster than
O(n2).)
Instructions
What intermediate computation result can be reused?