Easy to understand solution using queue


#1

int Solution::canJump(vector &ar) {
int n = ar.size();
bool *visited = new bool[n];
for(int i = 0;i < n;i++)
visited[i] = false;

    queue<int> q;
    q.push(0);
    visited[0] = true;
    int cou = 0;
    int flag1 = 0;
    int maxval = 0;
    while(!q.empty())
    {
        int x = q.size();
        while(x--)
        {
            int tt = q.front();
            q.pop();
            if(tt == n-1)
            {
                flag1 = 1;
                break;
            }
            for(int i = max(maxval,tt);i < n && i <= tt + ar[tt];i++)
            {
                if(!visited[i])
                {
                    maxval = max(maxval,i);
                    q.push(i);
                    visited[i] = true;
                }
            }
            
        }
        if(flag1 == 1)
        break;
        cou++;
    }
    if(flag1 == 1)
    return 1;
    else
    return 0;

}