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

Poisonous Plants

There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan

View Solution →

AND xor OR

Given an array of distinct elements. Let and be the smallest and the next smallest element in the interval where . . where , are the bitwise operators , and respectively. Your task is to find the maximum possible value of . Input Format First line contains integer N. Second line contains N integers, representing elements of the array A[] . Output Format Print the value

View Solution →

Waiter

You are a waiter at a party. There is a pile of numbered plates. Create an empty answers array. At each iteration, i, remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible ith the prime number. If it is, stack it in pile Bi. Otherwise, stack it in stack Ai. Store the values Bi in from top to bottom in answers. In the next iteration, do the

View Solution →

Queue using Two Stacks

A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que

View Solution →

Castle on the Grid

You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):

View Solution →

Down to Zero II

You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations N on in each move: 1: If we take 2 integers a and b where , N = a * b , then we can change N = max( a, b ) 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0. Input Format The first line contains the integer Q.

View Solution →