Chief Hopper


Problem Statement :


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 . 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  is less than the height of the building, his 
If the bot's  is greater than the height of the building, his



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 


Input Format

The first line contains an integer , the number of buildings.

The next line contains  space-separated integers , the heights of the buildings.



Solution :



title-img


                            Solution in C :

In  C :





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

int main() {
  int n, *h, i;
  unsigned long long tot;
  
  scanf("%d",&n);
  h = malloc(n * sizeof(int));
  for (i=0; i<n; i++) scanf("%d",&h[i]);
  
  tot = 0;
  i--;
  while (i>=0) {
    tot += h[i];
    if (tot & 1) tot++;
    tot /= 2;
    i--;
  }
  
  printf("%lld\n",tot);
  
  return 0;
}
                        


                        Solution in C++ :

In  C++  :







#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <limits>
#include <cstring>
#include <string>
using namespace std;

#define pairii pair<int, int>
#define llong long long
#define pb push_back
#define sortall(x) sort((x).begin(), (x).end())
#define INFI  numeric_limits<int>::max()
#define INFLL numeric_limits<llong>::max()
#define INFD  numeric_limits<double>::max()
#define FOR(i,s,n) for (int (i) = (s); (i) < (n); (i)++)
#define FORZ(i,n) FOR((i),0,(n))

const int MAXN = 100005;
int ar[MAXN];

void solve() {
  int n;
  scanf("%d",&n);
  FORZ(i,n) scanf("%d",ar+i);
  int res = 0;
  for (int i = n-1; i >= 0; i--) {
    int x = res + ar[i];
    res = x/2 + x%2;
  }
  printf("%d",res);
}

int main() {
#ifdef DEBUG
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  solve();
  return 0;
}
                    


                        Solution in Java :

In  Java :





import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		String s = in.nextLine();
		int[] heights = new int[n];
		for(int i = 0; i < n; i++) {
			heights[i] = in.nextInt();
		}
		System.out.println(calcMinEnergy(heights));
	}

	public static long calcMinEnergy(int[] heights) {
		if(heights.length < 1) return 0;
		long energy = 0;
		for(int i = 0; i < heights.length; i++) {
			long tmp = energy + heights[heights.length - 1 - i];
			int one = (int)(tmp % 2);
			energy = tmp / 2 + one;
		}
		return energy;
	}
}
                    


                        Solution in Python : 
                            
In  Python3 :





N = int(input())
heights = [int(n) for n in input().split()]

max_h = max(heights)

interval = [1, max_h]

def get_final_energy(e, heights):
	for h in heights:
		e = 2 * e - h
	return e

while interval[0] < interval[1] - 1:
	mid = (interval[0] + interval[1]) // 2
	fe = get_final_energy(mid, heights)
	if fe >= 0:
		interval[1] = mid
	else:
		interval[0] = mid

if get_final_energy(interval[0], heights) >= 0:
	print(interval[0])
else:
	print(interval[1])
                    


View More Similar Problems

Balanced Forest

Greg has a tree of nodes containing integer data. He wants to insert a node with some non-zero integer value somewhere into the tree. His goal is to be able to cut two edges and have the values of each of the three new trees sum to the same amount. This is called a balanced forest. Being frugal, the data value he inserts should be minimal. Determine the minimal amount that a new node can have to a

View Solution →

Jenny's Subtrees

Jenny loves experimenting with trees. Her favorite tree has n nodes connected by n - 1 edges, and each edge is ` unit in length. She wants to cut a subtree (i.e., a connected part of the original tree) of radius r from this tree by performing the following two steps: 1. Choose a node, x , from the tree. 2. Cut a subtree consisting of all nodes which are not further than r units from node x .

View Solution →

Tree Coordinates

We consider metric space to be a pair, , where is a set and such that the following conditions hold: where is the distance between points and . Let's define the product of two metric spaces, , to be such that: , where , . So, it follows logically that is also a metric space. We then define squared metric space, , to be the product of a metric space multiplied with itself: . For

View Solution →

Array Pairs

Consider an array of n integers, A = [ a1, a2, . . . . an] . Find and print the total number of (i , j) pairs such that ai * aj <= max(ai, ai+1, . . . aj) where i < j. Input Format The first line contains an integer, n , denoting the number of elements in the array. The second line consists of n space-separated integers describing the respective values of a1, a2 , . . . an .

View Solution →

Self Balancing Tree

An AVL tree (Georgy Adelson-Velsky and Landis' tree, named after the inventors) is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. We define balance factor for each node as : balanceFactor = height(left subtree) - height(righ

View Solution →

Array and simple queries

Given two numbers N and M. N indicates the number of elements in the array A[](1-indexed) and M indicates number of queries. You need to perform two types of queries on the array A[] . You are given queries. Queries can be of two types, type 1 and type 2. Type 1 queries are represented as 1 i j : Modify the given array by removing elements from i to j and adding them to the front. Ty

View Solution →