Header Ad

HackerEarth Jump Out problem solution

In this HackerEarth Jump Out problem solution, You have a field of N boosters. The power of ith booster is given as Ai. If you stand on ith booster, you will get the power Ai and you can make a jump of length Ai. That is, if you are standing on 3rd booster and it's power is 5, then you will land on position 8. Currently, you are standing outside the field to the left of first booster and you want to across this field by reaching to the right of Nth booster. You want to make only one jump to any booster such that you will finally land to right of Nth booster. Print the minimum length of initial jump you should make such that you will finally land outside the field by using exactly one booster.


HackerEarth Jump Out problem solution


HackerEarth Jump Out problem solution.

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fre freopen("in.txt","r",stdin)
#define pii pair<pair<ll,ll>, ll>
#define f first
#define s second
#define rep(i,n) for(int i=0;i<n;i++)
#define pb push_back
int A[100011];
int main() {
freopen("in09.txt","r",stdin);
freopen("out09.txt","w",stdout);
int N;
cin >> N;
for(int i=1;i<=N;i++) {
cin >> A[i];
}
for(int i=1;i<=N;i++) {
if(i+A[i] > N) {
cout << i;
return 0;
}
}
}


Second solution

n = int(raw_input())
arr = map(int, raw_input().split())
print next(i for i in range(n) if arr[i] + i >= n) + 1


Post a Comment

0 Comments