AMAZON Coding Question β Solved
A user is using the Amazon fitness tracker and is engaged in a jumping exercise routine. The user is positioned on the ground, and there are n blocks, each placed at different heights. The height of the ith block is represented by height[i] feet.
The goal is to maximize the calorie burn during this exercise. The calories burned when jumping from the ith block to the jth block is calculated as (height[i] - height[j])Β².
The user intends to jump on each block exactly once but can choose the order in which they jump. Since the user wants to optimize calorie burn for this session, the task is to determine the **maximum amount of calories** that can be burned during the exercise.
Notes:
- The user can jump from any block to any block.
- The ground's height is 0.
- Once the user jumps from the ground to a block, they **cannot** go back to the ground.
Example:
n = 3
height = [5, 2, 5]
The user can jump in this sequence: Ground β 3rd block β 2nd block β 1st block
Calories burned:
(0 - 5)Β² + (5 - 2)Β² + (2 - 5)Β² = 25 + 9 + 9 = 43
It can be shown that no other order results in more than 43 units of calorie burn. Hence, the answer is 43.
Function Description:
Complete the function `optimizeCalorieBurn` in the editor below.
Function Signature:
`int optimizeCalorieBurn(int[] height)`