Marks :10
: 2 | : 1
There are a series of $$$n$$$ connected mountains whose heights are represented in an array $$$arr[]$$$. A person is initially at zero height. The energy required for a person to climb up a mountain is difference between height of that mountain and person current height. No energy is needed to go down any mountain of any height (i.e lesser height than your current height).Calculate the total energy required by a person to climb all the mountains.
The first line contains an $$$(1 \le n \le 10^{6})$$$ representing the size of array.
The second line contains $$$n$$$ space separated integers $$$(0 \le arr_i \le 10^{9}) $$$ where $$$(0\le i < n)$$$
Print the single integer representing the total energy required by the person to climb all the mountains.
7 2 3 5 11 4 7 10
17
In first test case
size of array =7
$$$arr[]=[2,3,5,11,4,7,10]$$$
The person is initially at height=0.
To climb the 1st mountain of height =2 he required (2-0) = 2 units of energy.
To go to next hill of height=3 he requires (3-2) =1 unit of energy.
For 3rd and 4th hill, he requires (5-3) =2 units and (11-5)=6 units of energy resp.
Now to get to 5th hill, he does not require any energy as he is already at a height greater than the fifth height.
And for 6th and 7th hill, he requires (7-4)=3 units and (10-7)=3 units of energy resp.
Total energy = 2+1+2+6+0+3+3=17
You need to login to view your submissions.
You need to login to view all submissions.
Result : Executed
Feel something is wrong with the test cases?
Result : Accepted
Test Cases :
But to Run or Submit the Problem, you need to Log In.
Continue to Log InYour challenge has been submitted successfully.
You will get a response soon via WhatsApp or Email.
Do let us know your issue.