Which technically means it does not work. If your algorithm is dependent upon its input in order to produce the correct output, it is wrong, period.
Tarun Jha wrote:...only works some time
You should be able to do this entire exercise using pencil and paper before ever committing anything to code. Short of that and you are just guessing (hoping) at best.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles