1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

bool CanJump(int A[], int n) 
{
    int k = A[0];
    int i = 1;
	//k starts at 0, so '=' is included
	//n starts at 1
    while(i <= k && i < n)
    {
        if (k < (A[i]+i)) k =(A[i]+i); //furthest we could jump so far
        ++i;
    }

    return i>=n; 
}



#include <stdio.h>


int main(int argc, char** argv)
{
    int a[] = {2,3,1,1,4};
    int n = sizeof(a)/sizeof(int);
    printf("%d\n", CanJump(a,n)); //true

    int b[] = {3,2,1,0,4};
    n = sizeof(b)/sizeof(int);
    printf("%d\n", CanJump(b,n)); //false
    return 0;
}
View Program Text


Test Status