Given n integers in a maze indicating number of moves to be made from that position and a string which has “>” and “<" indicating which side to move. Starting position is first position.
Print whether it stays inside the array or moves out of the array.
**Example:**

Input : 3 2 1 1 > > < Output: It stays inside foreverExplanation:It moves towards right by a position of 2, hence is at the last index, then it moves to the left by 1, and then it again moves to the right by 1. Hence it doesn't go out. Input: 2 1 2 > < Output: comes outExplanation:Starts at 0th index, moves right by 1 position, and then moves left by 2 to come out

**Approach** to the above problem is as follows:

We start from 0th index and move until we exceed n or decreased 0. If we reach at the same position twice then we are in an infinite loop and can never move out.

* use mark array to mark the visited positions

* start from 0th index and check the sign of move and move to that place marking that position as visited

* if visited we can never move out , hence break out

* check the reason of break from loop , and print the desired result.

// below is the python implementation of the above approach

## Java

`//Java Possibility of moving out of maze ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to check whether it ` ` ` `// will stay inside or come out ` ` ` `static` `void` `checkingPossibility( ` `int` `a[], ` `int` `n, String s) ` ` ` `{ ` ` ` `// marks all the positions that is visited ` ` ` `int` `mark[] = ` `new` `int` `[a[` `0` `] * n] ; ` ` ` ` ` `// Initial starting point ` ` ` `int` `start = ` `0` `; ` ` ` ` ` `// initial assumption is it comes out ` ` ` `int` `possible = ` `1` `; ` ` ` ` ` `//runs till it is inside or comes out ` ` ` `while` `( start >= ` `0` `&& start < n) ` ` ` `{ ` ` ` ` ` `//if the movement is towards left ` ` ` `//then we move left. The start variable ` ` ` `// and mark that position as visited ` ` ` `// if not visited previously. Else we ` ` ` `// break out ` ` ` `if` `(s == ` `"<"` `) ` ` ` `{ ` ` ` ` ` `if` `(mark[start] == ` `0` `) ` ` ` `{ ` ` ` `mark[start] = ` `1` `; ` ` ` `start -= a[start] ; ` ` ` `} ` ` ` ` ` `// It will be inside forever ` ` ` `else` `{ ` ` ` `possible = ` `0` `; ` ` ` `break` `;} ` ` ` `} ` ` ` ` ` `// If the movement is towards right, then ` ` ` `// we move right. The start variable and ` ` ` `// mark that position as visited if not ` ` ` `// visited previously else we break out ` ` ` `else` ` ` `{ ` ` ` `if` `(mark[start] == ` `0` `) ` ` ` `{ ` ` ` `mark[start] = ` `1` `; ` ` ` `start += a[start] ; ` ` ` `} ` ` ` ` ` `// it will be inside forever ` ` ` `else` ` ` `{ ` ` ` `possible = ` `0` `; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `if` `(possible == ` `0` `) ` ` ` `System.out.print( ` `"it stays inside forever"` `); ` ` ` `else` ` ` `System.out.print (` `"comes out"` `); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `2` `; ` ` ` `String s = ` `"><"` `; ` ` ` `int` `a[] = {` `1` `, ` `2` `}; ` ` ` `checkingPossibility(a, n, s); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## Python3

`# Function to check whether it will stay inside ` `# or come out ` `def` `checkingPossibility(a, n, s): ` ` ` ` ` `# marks all the positions that is visited ` ` ` `mark ` `=` `[` `0` `] ` `*` `n ` ` ` ` ` `# Initial starting point ` ` ` `start ` `=` `0` ` ` ` ` `# initial assumption is it comes out ` ` ` `possible ` `=` `1` ` ` ` ` `# runs till it is inside or comes out ` ` ` `while` `start >` `=` `0` `and` `start < n: ` ` ` ` ` `# if the movement is towards left ` ` ` `# then we move left. The start variable ` ` ` `# and mark that position as visited ` ` ` `# if not visited previously. Else we ` ` ` `# break out ` ` ` `if` `s[start] ` `=` `=` `"<"` `: ` ` ` ` ` `if` `mark[start] ` `=` `=` `0` `: ` ` ` `mark[start] ` `=` `1` ` ` `start ` `-` `=` `a[start] ` ` ` ` ` `# It will be inside forever ` ` ` `else` `: ` ` ` `possible ` `=` `0` ` ` `break` ` ` ` ` `# If the movement is towards right, then ` ` ` `# we move right. The start variable and ` ` ` `# mark that position as visited if not ` ` ` `# visited previously else we break out ` ` ` `else` `: ` ` ` `if` `mark[start] ` `=` `=` `0` `: ` ` ` `mark[start] ` `=` `1` ` ` `start ` `+` `=` `a[start] ` ` ` ` ` `# it will be inside forever ` ` ` `else` `: ` ` ` `possible ` `=` `0` ` ` `break` ` ` ` ` `if` `possible ` `=` `=` `0` `: ` ` ` `print` `"it stays inside forever"` ` ` `else` `: ` ` ` `print` `"comes out"` ` ` `# Driver code ` `n ` `=` `2` `s ` `=` `"><"` `a ` `=` `[` `1` `, ` `2` `] ` `checkingPossibility(a, n, s) ` |

*chevron_right*

*filter_none*

## C#

`// C# Possibility of moving out of maze ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to check whether it ` ` ` `// will stay inside or come out ` ` ` `static` `void` `checkingPossibility( ` `int` `[]a, ` ` ` `int` `n, String s) ` ` ` `{ ` ` ` ` ` `// marks all the positions that ` ` ` `// is visited ` ` ` `int` `[]mark = ` `new` `int` `[a[0] * n] ; ` ` ` ` ` `// Initial starting point ` ` ` `int` `start = 0; ` ` ` ` ` `// initial assumption is it ` ` ` `// comes out ` ` ` `int` `possible = 1; ` ` ` ` ` `//runs till it is inside or ` ` ` `// comes out ` ` ` `while` `( start >= 0 && start < n) ` ` ` `{ ` ` ` ` ` `//if the movement is towards ` ` ` `// left then we move left. ` ` ` `// The start variable and ` ` ` `// mark that position as ` ` ` `// visited if not visited ` ` ` `// previously. Else we ` ` ` `// break out ` ` ` `if` `(s == ` `"<"` `) ` ` ` `{ ` ` ` ` ` `if` `(mark[start] == 0) ` ` ` `{ ` ` ` `mark[start] = 1; ` ` ` `start -= a[start] ; ` ` ` `} ` ` ` ` ` `// It will be inside forever ` ` ` `else` ` ` `{ ` ` ` `possible = 0; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// If the movement is towards ` ` ` `// right, then we move right. ` ` ` `// The start variable and mark ` ` ` `// that position as visited if ` ` ` `// not visited previously else ` ` ` `// we break out ` ` ` `else` ` ` `{ ` ` ` `if` `(mark[start] == 0) ` ` ` `{ ` ` ` `mark[start] = 1; ` ` ` `start += a[start] ; ` ` ` `} ` ` ` ` ` `// it will be inside forever ` ` ` `else` ` ` `{ ` ` ` `possible = 0; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `if` `(possible == 0) ` ` ` `Console.Write( ` `"it stays "` ` ` `+ ` `"inside forever"` `); ` ` ` `else` ` ` `Console.Write(` `"comes out"` `); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` ` ` `int` `n = 2; ` ` ` `String s = ` `"><"` `; ` ` ` `int` `[]a = {1, 2}; ` ` ` ` ` `checkingPossibility(a, n, s); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

Output:

comes out

Time complexity: O(n)

This article is contributed by **Twinkl Bajaj**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Possibility of a word from a given set of characters
- Count ways to place Knights moving in L shape in chessboard
- Minimum operations to transform given string to another by moving characters to front or end
- Find out the correct position of the ball after shuffling
- Number of ways in which the substring in range [L, R] can be formed using characters out of the range
- Costs required to move all 1s to each index of a given Binary Array
- Count array elements having modular inverse under given prime number P equal to itself
- Print alternate elements of an array
- Smallest subsequence having GCD equal to GCD of given array
- Print Nodes which are not part of any cycle in a Directed Graph
- Minimize deletions in a Binary String to remove all subsequences of the form "0101"
- Length of longest subsequence having sum of digits of each element as a Composite Number
- Find K that requires minimum increments or decrements of array elements to obtain a sequence of increasing powers of K
- Length of longest subarray with product equal to a power of 2