I was solving this particular problem in August Lunchtime challenge, but when I submitted the solution I got Wrong Answer. [(My submission code link.)]
My strategy to solve the problem is:
- Loop through the array.
- Select two adjacent integers.
- Find minimum of them and discard the other one.
cost += min;(Cost of this operation will be equal to the smaller of them.)
- Display the cost.
I get testcases (mentioned on the problem page) as correct on my local IDE. But when I submit the solution I get WA.
As the contest ended, I went through the solution of other members, and I was confused with the following points:
- Challengers found out the minimum number in the array(so did I), but they did not increment the cost variable(as the constrain says Cost of this operation will be equal to the smaller of them.)
- At the end they have multiplied the smallest number in the array with
(n-1)and displayed it as the final output.
Clearly I have misunderstood the problem statement(even after spending ample amount of time on it!). I request you geeks in sorting my problem, as why we are not supposed to increment cost variable with min number throughout the loop, what is the significance of