Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
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

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

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