Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

HackerRank Chief Hopper problem solution

YASH PAL, 31 July 202425 January 2026

In this 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.

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 botEnergy
HackerRank Chief Hopper problem solution

HackerRank Chief Hopper problem solution in Python.

n = int(input())
high = low = 0
arr = []

for i in input().split():
arr.append(int(i))
if(int(i) > high):
high = int(i)
if(int(i) < low):
low = int(i)

def tester(x, arr):
energy = x
answer = True
for height in arr:
if(height > energy):
energy -= (height - energy)
if(energy < 0):
return(False)
else:
energy += (energy - height)
return(answer)

cont = True
i = (high + low) // 2
dic = {}
while(cont):
if(dic.get(i-1, tester(i-1, arr))):
# i is too high
high = i
i = (high + low) // 2
else:
if(dic.get(i, tester(i, arr))):
cont = False
else:
# i is too low, double it and set low to current i
low = i
i = ((i + high + 1) // 2)
if(i == 0):
i = 1

print(i)


Chief Hopper problem solution in Java.

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);
    }
}

Problem solution in C++.

//gskhirtladze

#include <iostream>
#include <algorithm>
#include <stdio.h>

#define LL long long
#define getcx getchar//_unlocked

inline void inp( int &n )
 { n=0;int ch=getcx();
   while(ch<'0'||ch>'9') ch=getcx();
   while(  ch >= '0' && ch <= '9' )
      n = (n<<3)+(n<<1) + ch-'0', ch=getcx(); }  

using namespace std;

int n,i,H[100020];
LL ans;

int main(){inp(n);
for (i=1;i<=n;i++) inp(H[i]);
reverse(H+1,H+n+1);
for (i=1;i<=n;i++)
 ans=(ans+(LL)H[i]+1)/2LL;
cout<<ans<<endl;}

Problem solution in C.

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

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    int b[100000],n,i,ans;
    scanf("%d",&n);
    for(i=0;i<n;i++){
        scanf("%d",&b[i]);
    }
    ans=0;
    for(i=n-1;i>=0;i--){
        ans=(ans+b[i])/2+(ans+b[i])%2;
    }
    printf("%d",ans);
    return 0;
}

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes