Chetan and his Crush(Very-Easy)

Chetan has a crush on a girl and purposes her but the girl is quite adventurous. So she will accept Chetan's proposal only if he can cross a river using one jump.

The river has n stones placed and each stone has a maximum power associated with it which means if Chetan lands on the ith stone which has power A[i] then he can make another jump of A[i] length. Currently, Chetan is on the left side of the river and he has to cross the river by landing only on one stone. Print the minimum length of the initial jump you should make such that you will finally land outside the field by using exactly one booster. Print the minimum length of jump he should make so that he will land on another side of the river by using one stone. 

Example:

Input:  n = 5, a = [4, 2, 4, 2, 3]
Output: 3

Approach

C++

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

int minimumJumps(int nint a[])
{
    int res = nl = 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 << minimumJumps(na<< "\n";

    return 0;
}


No comments:

Post a Comment