Jump Out

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 cross 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.

Example:

Input:  n = 5, a = {4, 2, 4, 2, 3}
Output: 3

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int jumpOut(int nint a[])
{
    int res = n;
    int l = 0;
    for (int i = n - 1i >= 0i--)
    {
        if ((a[i] + i - n) >= 0)
        {
            res = min(resn - l);
        }
        l++;
    }
    return res;
}
int main()
{
    int n = 5;

    int a[n] = {42423};

    cout << jumpOut(na<< "\n";

    return 0;
}


No comments:

Post a Comment