HackerRank: Chief Hopper
static boolean test(int A[], int init, int max){
long energy = init;
for(int i = 0; i < A.length; i++){
energy += (energy - A[i]);
if(energy < 0)
return false;
if(energy >= max){
return true;
}
}
return true;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N;
N = in.nextInt();
int buildings[] = new int[N];
int min = 100006;
int max = -1;
for(int i = 0; i < N; i++){
buildings[i] = in.nextInt();
min = Math.min(min, buildings[i]);
max = Math.max(max, buildings[i]);
}
int l = 0;
int r = max;
while(l < r){
int mid = (l+r)/2;
if(test(buildings, mid, max)){
r = mid;
}else{
l = mid + 1;
}
}
if(l == r){
System.out.println(l);
}else if(test(buildings, r, max)){
System.out.println(r);
}else{
System.out.println(l);
}
Method 3
by SalvadorDali
If we have starting energyx and we jump on the building a
our energy will be2∗x−a
This is enough to figure out that if after thea1,a2,⋯an
s=a1∗2(n−1)+a2∗2(n−2)+...+an
ceil(s/2n)
to decrease the number of the computations, after division we will get:
a12+a24+⋯+an2n
then take the ceil to get the integer. This answer is correct if we have infinite precision.
In case of computers, where this is not the case, we need to check whether the answer is correct (it can be of by 1)
first for loop isO(n) , second check is O(n) . thus solution is O(n)
Chief's bot is playing an old DOS-based game. There are N+1 buildings in the game - indexed from 0 to N and are placed left-to-right. It is guaranteed that building with index 0 will be of height 0 unit. For buildings with index i (i∈[1,N] ) height will be hi units.
At beginning Chief's bot is at building with index0 . At each step, bot jumps to next (right) building. Suppose bot is at kth building and his current energy is botEnergy , then in next step he will jump to (k+1)th building. He will gain/lose energy equal in amount to difference between hk+1 and botEnergy
At beginning Chief's bot is at building with index
- If
hk+1>botEnergy , then he will losehk+1−botEnergy units of energy. - Otherwise, he will gain
botEnergy−hk+1 units of energy.
Goal is to reach Nth building, and during the course bot should never have negative energy units. What should be the minimum units of energy with which bot should start to successfully complete the game?
Using Binary search, you can try each number and checking if it goes negative, there is a high chance number can overflow integer ranges, you can keep upper limit to max height of any building.
int main() {
int n;
cin>>n;
vector <long long int> Arr(n);
for (int i=0;i<n;i++) {
cin>>Arr[i];
}
long long int beg = Arr[n-1]/2 + Arr[n-1]%2;
for (int i = n-2;i>=0;i--) {
beg = (Arr[i] + beg)/2 + (Arr[i] + beg)%2;
}
cout<<beg<<endl;
return 0;
}
long energy = init;
for(int i = 0; i < A.length; i++){
energy += (energy - A[i]);
if(energy < 0)
return false;
if(energy >= max){
return true;
}
}
return true;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N;
N = in.nextInt();
int buildings[] = new int[N];
int min = 100006;
int max = -1;
for(int i = 0; i < N; i++){
buildings[i] = in.nextInt();
min = Math.min(min, buildings[i]);
max = Math.max(max, buildings[i]);
}
int l = 0;
int r = max;
while(l < r){
int mid = (l+r)/2;
if(test(buildings, mid, max)){
r = mid;
}else{
l = mid + 1;
}
}
if(l == r){
System.out.println(l);
}else if(test(buildings, r, max)){
System.out.println(r);
}else{
System.out.println(l);
}
This problem can be solved assuming the fact when you reach the last building your energy can be at least 0. which means suppose last height is hn your energy at hn−1 must be greater than or equal to hn2
Applying this till we reach h0
Method 3
by SalvadorDali
If we have starting energy
our energy will be
This is enough to figure out that if after the
to decrease the number of the computations, after division we will get:
In case of computers, where this is not the case, we need to check whether the answer is correct (it can be of by 1)
first for loop is