Chief Hopper HackerRank Solution

Chief Hopper HackerRank Solution

In this Chief Hopper HackerRank solution, Chief’s bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building  and at a height of . We must need to  determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero.

Units of height relate directly to units of energy. The bot’s energy level is calculated as follows:

  • If the bot’s  is less than the height of the building, his 
  • If the bot’s  is greater than the height of the building, his 

Chief Hopper HackerRank solution

I will Provide solution in Multiple programming languages for you. If you are not able to find the code in required language then please share in comments so that our team can help you.

Problem Solution in Python

def chiefHopper(arr):
    energy = 0
    for height in reversed(arr):
        energy = math.ceil((energy + height) / 2)
    return energy

Problem Solution in Java

 public static int chiefHopper(List<Integer> arr) {
        // by the time we get to last building we have energy >=0
        // so starting with 0 from last to get min energy at beginning
   
        // botEnergy = (newEnergy+ height) / 2
        // energy = Math.ceil((energy + height) /2);


    int energy = 0;    
    for(int i = arr.size()-1; i >=0; i--){
        energy = (int)Math.ceil((double)(energy + arr.get(i)) /2);
    }
    return energy;
    }

Problem Solution in C++

int chiefHopper(vector<int> arr) {
    float e=0;
    for(int i=arr.size()-1;i>=0;i--)
    {
        e=ceil((e+arr[i])/2);
    }
    return e;
}

Problem Solution in JavaScript

function chiefHopper(arr) {
    let b;
    let energy = 0;
    while ((b = arr.pop()) !== undefined) {
        energy = Math.ceil((energy + b) / 2);
    }
    return energy;
}
Solve original Problem on HackerRank here. Checkout more HackerRank Problems