Given two integers **S** and **T** and an array **arr** that contains elements from **1** to **N** in unsorted fashion. The task is to find the minimum number of moves to move **Sth** element to the **Tth** place in the array with the following operation:

A single move consists of the following

// Initially b[] = {1, 2, 3, ..., N} // arr[] is input array for (i = 1..n) temp[arr[i]] = b[i] b = temp

If not possible then print **-1** instead.

**Examples:**

Input:S = 2, T = 1, arr[] = {2, 3, 4, 1}

Output:3

N is 4 (size of arr[])

Move 1: b[] = {4, 1, 2, 3}

Move 2: b[] = {3, 4, 1, 2}

Move 3: b[] = {2, 3, 4, 1}

Input:S = 3, T = 4, arr[] = {1, 2, 3, 4}

Output:-1

N is 4 (Size of arr[])

Regardless of how many moves are made, the permutation would remain the same.

**Approach:** The important observation here is that we are only concerned with the position of a single element, and not the entire array. So at each move we move the element at position **S** to the position **arr[S]**, until we reach **Tth** position.

Since there are at most **N** distinct places that we can reach, if we don’t reach **T** within **N** moves, it would mean we can never reach it.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the number of moves ` `int` `minimumMoves(` `int` `n, ` `int` `a[], ` `int` `s, ` `int` `t) ` `{ ` ` ` `int` `i, x; ` ` ` `x = s; ` ` ` `for` `(i = 1; i <= n; i++) { ` ` ` `if` `(x == t) ` ` ` `break` `; ` ` ` `x = a[x]; ` ` ` `} ` ` ` ` ` `// Destination reached ` ` ` `if` `(x == t) ` ` ` `return` `i - 1; ` ` ` `else` ` ` `return` `-1; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `s = 2, t = 1, i; ` ` ` `int` `a[] = {-1, 2, 3, 4, 1}; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` `cout << minimumMoves(n, a, s, t); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the appraoch ` `public` `class` `GFG{ ` ` ` `// Function to return the number of moves ` `static` `int` `minimumMoves(` `int` `n, ` `int` `a[], ` `int` `s, ` `int` `t) ` `{ ` ` ` `int` `i, x; ` ` ` `x = s; ` ` ` `for` `(i = ` `1` `; i <= n; i++) { ` ` ` `if` `(x == t) ` ` ` `break` `; ` ` ` `x = a[x]; ` ` ` `} ` ` ` ` ` `// Destination reached ` ` ` `if` `(x == t) ` ` ` `return` `i - ` `1` `; ` ` ` `else` ` ` `return` `-` `1` `; ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String []args){ ` ` ` `int` `s = ` `2` `, t = ` `1` `, i; ` ` ` `int` `a[] = {-` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `1` `}; ` ` ` `int` `n = a.length ; ` ` ` `System.out.println(minimumMoves(n, a, s, t)); ` ` ` `} ` ` ` `// This code is contributed by Ryuga ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the number of moves ` `def` `minimumMoves(n, a, s, t): ` ` ` ` ` `x ` `=` `s ` ` ` `for` `i ` `in` `range` `(` `1` `, n` `+` `1` `): ` ` ` `# Destination reached ` ` ` `if` `x ` `=` `=` `t: ` ` ` `return` `i` `-` `1` ` ` `x ` `=` `a[x] ` ` ` ` ` `return` `-` `1` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `s, t ` `=` `2` `, ` `1` ` ` `a ` `=` `[` `-` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `1` `] ` ` ` `n ` `=` `len` `(a) ` ` ` `print` `(minimumMoves(n, a, s, t)) ` ` ` `# This code is contributed by Rituraj Jain ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` `public` `class` `GFG{ ` ` ` `// Function to return the number of moves ` `static` `int` `minimumMoves(` `int` `n, ` `int` `[]a, ` `int` `s, ` `int` `t) ` `{ ` ` ` `int` `i, x; ` ` ` `x = s; ` ` ` `for` `(i = 1; i <= n; i++) { ` ` ` `if` `(x == t) ` ` ` `break` `; ` ` ` `x = a[x]; ` ` ` `} ` ` ` ` ` `// Destination reached ` ` ` `if` `(x == t) ` ` ` `return` `i - 1; ` ` ` `else` ` ` `return` `-1; ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main(){ ` ` ` `int` `s = 2, t = 1; ` ` ` `int` `[]a = {-1, 2, 3, 4, 1}; ` ` ` `int` `n = a.Length ; ` ` ` `Console.WriteLine(minimumMoves(n, a, s, t)); ` ` ` `} ` ` ` `// This code is contributed by inder_verma. ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the appraoch ` ` ` ` ` `// Function to return the number of moves ` `function` `minimumMoves(` `$n` `, ` `$a` `, ` `$s` `, ` `$t` `) ` `{ ` ` ` `$i` `; ` `$x` `; ` ` ` `$x` `= ` `$s` `; ` ` ` `for` `(` `$i` `= 1; ` `$i` `<= ` `$n` `; ` `$i` `++) { ` ` ` `if` `(` `$x` `== ` `$t` `) ` ` ` `break` `; ` ` ` `$x` `= ` `$a` `[` `$x` `]; ` ` ` `} ` ` ` ` ` `// Destination reached ` ` ` `if` `(` `$x` `== ` `$t` `) ` ` ` `return` `$i` `- 1; ` ` ` `else` ` ` `return` `-1; ` `} ` ` ` `// Driver Code ` ` ` ` ` `$s` `= 2; ` `$t` `= 1; ` `$i` `; ` ` ` `$a` `= ` `array` `(-1, 2, 3, 4, 1); ` ` ` `$n` `= ` `count` `(` `$a` `); ` ` ` `echo` `minimumMoves(` `$n` `, ` `$a` `, ` `$s` `, ` `$t` `); ` ` ` `// This code is contributed by inder_verma. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

3

## Recommended Posts:

- Minimum number of moves to make all elements equal
- Minimum number of changes required to make the given array an AP
- How to evenly put N objects into N places with adjacent moves
- Print all paths from top left to bottom right in a matrix with four moves allowed
- Change if all bits can be made same by single flip
- Reduce the array to a single element with the given operation
- Position of n among the numbers made of 2, 3, 5 & 7
- Count all possible position that can be reached by Modified Knight
- Find minimum x such that (x % k) * (x / k) == n
- Minimum absolute difference between N and a power of 2
- Minimum odd cost path in a matrix
- Minimum number of items to be delivered
- Minimum array element changes to make its elements 1 to N
- Dijkstra's shortest path with minimum edges
- Sum of minimum difference between consecutive elements of an array

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.