Advertisement
imashutosh51

Check if the end of the Array can be reached from a given position

Nov 10th, 2022 (edited)
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. /*
  2. https://www.geeksforgeeks.org/check-if-the-end-of-the-array-can-be-reached-from-a-given-position/
  3. This whole code is GFG article code so don't write this code anywhere,Modify the code.
  4.  
  5. Logic: BFS
  6. We are starting from S and there is two situation only either go s+arr[i] or s-arr[i].so now,we are having two new starting points if any of the above two reaches to end,then answer will be yes.Suppose we again reach at s+arr[i] after some steps then the results will be same.So keep a visited array where we don't need to come to the same point because nothing is going to change,you will follow the same path again you went earlier.
  7. T.C : O(N) as we move all i once.
  8. S.C : O(N) for queue for BFS
  9. */
  10.  
  11. // C++ program for the above approach
  12. #include <bits/stdc++.h>
  13. using namespace std;
  14.  
  15. // Function to check if we can reach to
  16. // the end of the arr[] with possible moves
  17. void solve(int arr[], int n, int start)
  18. {
  19.  
  20.     // Queue to perform BFS
  21.     queue<int> q;
  22.  
  23.     // Initially all nodes(index)
  24.     // are not visited.
  25.     bool visited[n] = { false };
  26.  
  27.     // Initially the end of
  28.     // the array is not reached
  29.     bool reached = false;
  30.  
  31.     // Push start index in queue
  32.     q.push(start);
  33.  
  34.     // Until queue becomes empty
  35.     while (!q.empty()) {
  36.  
  37.         // Get the top element
  38.         int temp = q.front();
  39.  
  40.         // Delete popped element
  41.         q.pop();
  42.  
  43.         // If the index is already
  44.         // visited. No need to
  45.         // traverse it again.
  46.         if (visited[temp] == true)
  47.             continue;
  48.  
  49.         // Mark temp as visited
  50.         // if not
  51.         visited[temp] = true;
  52.         if (temp == n - 1) {
  53.  
  54.             // If reached at the end
  55.             // of the array then break
  56.             reached = true;
  57.             break;
  58.         }
  59.  
  60.         // If temp + arr[temp] and
  61.         // temp - arr[temp] are in
  62.         // the index of array
  63.         if (temp + arr[temp] < n) {
  64.             q.push(temp + arr[temp]);
  65.         }
  66.  
  67.         if (temp - arr[temp] >= 0) {
  68.             q.push(temp - arr[temp]);
  69.         }
  70.     }
  71.  
  72.     // If reaches the end of the array,
  73.     // Print "Yes"
  74.     if (reached == true) {
  75.         cout << "Yes";
  76.     }
  77.  
  78.     // Else print "No"
  79.     else {
  80.         cout << "No";
  81.     }
  82. }
  83.  
  84. // Driver Code
  85. int main()
  86. {
  87.     // Given array arr[]
  88.     int arr[] = { 4, 1, 3, 2, 5 };
  89.  
  90.     // Starting index
  91.     int S = 1;
  92.     int N = sizeof(arr) / sizeof(arr[0]);
  93.  
  94.     // Function Call
  95.     solve(arr, N, S);
  96.  
  97.     return 0;
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement