My Code…
One case that you are missing is the case of n = 1.
Use MAP. That will be the most efficient way of coding this solution. Moreover if the elements were in the range of -ve numbers your algorithm may not work. I have coded using MAP and take a look at this
[1]. If you have any-doubt please do ask me :)
**HAPPY CODING**
[1]: http://ideone.com/xqGJeC
But A[i] is in the range [1,1000]. It never takes negative value.
Thanks…
No it does not take -ve values. I know it. I have done the quesion. But suppose in case in an interview you are asked the same question with the constraints as -10^4 to 10^4?? You will have to use map. So this code works for both. If you dont understand the syntax please do ask me
For that, I have to first learn map. So first of all I’ll see maps and then I’ll get back to you if I have any query.
Thanks for the help…