Advertisement
nikunjsoni

1306

Apr 25th, 2021
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     bool canReach(vector<int>& arr, int start) {
  4.         queue<int> q;
  5.         int sz = arr.size();
  6.         bool vis[arr.size()];
  7.         memset(vis, 0, sizeof(vis));
  8.        
  9.         q.push(start);
  10.         vis[start] = true;
  11.         while(!q.empty()){
  12.             int idx  = q.front();
  13.             q.pop();
  14.             if(arr[idx] == 0) return true;
  15.             int next = idx+arr[idx];
  16.             if(next < sz && next >= 0  && !vis[next]){
  17.                 q.push(next);
  18.                 vis[next] = true;
  19.             }
  20.             next = idx-arr[idx];
  21.             if(next < sz && next >= 0  && !vis[next]){
  22.                 q.push(next);
  23.                 vis[next] = true;
  24.             }
  25.         }
  26.         return false;
  27.     }
  28. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement