Skip to content
Programmingoneonone
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

  • Home
  • CS Subjects
    • IoT ? Internet of Things
    • Digital Communication
    • Human Values
  • 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

LEARN EVERYTHING ABOUT PROGRAMMING

HackerRank Chief Hopper problem solution

YASH PAL, 31 July 2024

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.

HackerRank Chief Hopper problem solution

Topics we are covering

Toggle
  • Problem solution in Python.
  • Problem solution in Java.
  • Problem solution in C++.
  • Problem solution in C.

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)

{“mode”:”full”,”isActive”:false}

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

{“mode”:”full”,”isActive”:false}

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

{“mode”:”full”,”isActive”:false}

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

{“mode”:”full”,”isActive”:false}

Algorithms coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes