Skip to content
thecscience
THECSICENCE

Learn everything about computer science

  • Home
  • Human values
  • NCERT Solutions
  • HackerRank solutions
    • HackerRank Algorithms problems solutions
    • HackerRank C solutions
    • HackerRank C++ solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
thecscience
THECSICENCE

Learn everything about computer science

HackerRank Chief Hopper Problem Solution

Yashwant Parihar, June 12, 2023August 1, 2024

In this post, we will solve HackerRank Chief Hopper Problem 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 0 and at a height of 0). You must 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 bot Energy is less than the height of the building, his
    newEnergy bot Energy (height – bot Energy) • If the bot’s bot Energy is greater than the height of the building, his newEnergy bot Energy + (bot Energy – height)
    Example
    arr = [2, 3, 4, 3, 2]
    Starting with bot Energy = 4, we get the following table:
  botEnergy   height  delta
    4               2       +2
    6               3       +3
    9               4       +5
    14              3       +11
    25              2       +23
    48

That allows the bot to complete the course, but may not be the minimum starting value.
The minimum starting bot Energy in this case is 3.
Function Description
Complete the chiefHopper function in the editor below.
chiefHopper has the following parameter(s):
int arr[n]: building heights
Returns
int: the minimum starting bot Energy
Input Format
The first line contains an integer n, the number of buildings.
The next line contains n space-separated integers arr[1].. arr[n], the heights of the
buildings.

Sample Input 0

5
3 4 3 2 4

Sample Output 0

4

Explanation 0

If initial energy is 4, after step 1 energy is 5, after step 2 it’s 6, after step 3 it’s 9 and after step 4 it’s 16, finally at step 5 it’s 28.
If initial energy were 3 or less, the bot could not complete the course.

Sample Input 1

3
4 4 4

Sample Output 1

4

Explanation 1

In the second test case if bot has energy 4, it’s energy is changed by (4 – 4 = 0) at every step and remains 4.

Sample Input 2

3
1 6 4

Sample Output 2

3

Explanation 2

botEnergy   height  delta
3           1       +2
5           6       -1
4           4       0
4

We can try lower values to assure that they won’t work.

HackerRank Chief Hopper Problem Solution
HackerRank Chief Hopper Problem Solution

Chief Hopper C Solution

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>


int main() {long long  n;
            scanf("%lld",&n);
            long long  arr[n];
            long long t;
            for(t=0;t<n;t++)
                scanf("%lld",&arr[t]);
          long long k=arr[0];
           long long p;
            int flag;
            long long c,e;
            for(c=1;c<100000;c++)
               {flag=1; e=c;
                for(p=0;p<n;p++)
                {
                e=2*e-arr[p];
                    if(e>1844674407370955161)
                        {break;
                        
                    }
                    
                if(e<0)
                    {
                    flag=0;
                     break;}
                
            }
            if(flag==1)
                break;}
            
                
            printf("%lld",c);
            
            
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    return 0;
}

Chief Hopper C++ Solution

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

unsigned required_energy(unsigned building_height, unsigned required){
    return (1 + required + building_height) / 2;
}
int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    
    auto n = 0;
    std::cin >> n;
    auto vec = std::vector<unsigned>(n);
    
    for (auto& x : vec)
        std::cin >> x;
    unsigned result = 0;
    for (auto it = vec.rbegin(); it != vec.rend(); it++){
        result = required_energy(*it, result);
    }
    std::cout << result << std::endl;
    return 0;
}

Chief Hopper C Sharp Solution

using System;
using System.Collections.Generic;
using System.Linq;

class Solution {
    static void Main(String[] args) {
                    var n = Convert.ToInt32(Console.ReadLine());
            var input = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();

            int energy = 0;

            for (int i = n - 1; i >= 0; i--)
            {
                var h = input[i];
                energy = (energy + h) / 2 + (energy + h) % 2;
            }

            Console.WriteLine(energy);
    }
}

Chief Hopper Java Solution

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) throws Exception {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s;
        StringTokenizer st;
        int N=Integer.parseInt(br.readLine().trim());
        s=br.readLine().trim();
        st=new StringTokenizer(s);
        int height[]=new int[N];
        for(int i=0;i<N;i++)
        {
            height[i]=Integer.parseInt(st.nextToken());
        }
        int min=0;
        for(int i=N-1;i>=0;i--)
        {
            min= (int) Math.ceil((double)(min+height[i])/2);
            
        }
        System.out.println(min);
    }
}

Chief Hopper Javascript Solution

function processData(input) {
    //Enter your code here
    input = input.split("\n");
    var n = parseInt(input[0]);
    var arr = input[1].split(" ").map(Number);
    
    var minBotEnergy = testEnergy(arr, n);
    
    console.log(minBotEnergy);
} 

function testEnergy(heights, total) {
  var botEnergy = 0;

  while(!runGame(botEnergy, heights, total)) {
    botEnergy++;
  }

  return botEnergy;
}

function runGame(botEnergy, heights, total) {
  var height;
  var diff;

  for (var i = 0; i<total; i++) {
      height = heights[i];

      diff = botEnergy-height;
      botEnergy += diff;

      if (botEnergy < 0) return false;
  }

  return true;
}


process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});

Chief Hopper Python Solution

import sys


if __name__ == '__main__':
    N = int(sys.stdin.readline())
    H = list(map(int, sys.stdin.readline().split()))

    energy = 0

    for i in reversed(range(N)):
        energy = (energy + H[i] + 1) // 2

    print(energy)
c C# C++ HackerRank Solutions java javascript python CcppCSharpHackerrank Solutionsjavajavascriptpython

Post navigation

Previous post
Next post
  • HackerRank Dynamic Array Problem Solution
  • HackerRank 2D Array – DS Problem Solution
  • Hackerrank Array – DS Problem Solution
  • Von Neumann and Harvard Machine Architecture
  • Development of Computers
©2025 THECSICENCE | WordPress Theme by SuperbThemes