# Find the minimum number of moves to reach end of the array

Given an array arr[] of size N where every element is from the range [0, 9]. The task is to reach the last index of the array starting from the first index. From ith index we can move to (i – 1)th, (i + 1)th or to any jth index where j ≠ i and arr[j] = arr[i].

Examples:

Input: arr[] = {1, 2, 3, 4, 1, 5}
Output: 2
First move from the 0th index to the 4th index
and then from the 4th index to the 5th.

Input: arr[] = {1, 2, 3, 4, 5, 1}
Output: 1

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Construct the graph from the given array where the number of nodes in the graph will be equal to the size of the array. Every node of the graph i will be connected to the (i 1)th node, (i + 1)th node and a node j such that i ≠ j and arr[i] = arr[j]. Now, the answer will be the minimum edges in the path from index 0 to index N – 1 in the constructed graph.
The graph for the array arr[] = {1, 2, 3, 4, 1, 5} is shown in the image below: Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` `#define N 100005 ` ` `  `vector<``int``> gr[N]; ` ` `  `// Function to add edge ` `void` `add_edge(``int` `u, ``int` `v) ` `{ ` `    ``gr[u].push_back(v); ` `    ``gr[v].push_back(u); ` `} ` ` `  `// Function to return the minimum path ` `// from 0th node to the (n - 1)th node ` `int` `dijkstra(``int` `n) ` `{ ` `    ``// To check whether an edge is visited or not ` `    ``// and to keep distance of vertex from 0th index ` `    ``int` `vis[n] = { 0 }, dist[n]; ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``dist[i] = INT_MAX; ` ` `  `    ``// Make 0th index visited and distance is zero ` `    ``vis = 1; ` `    ``dist = 0; ` ` `  `    ``// Take a queue and push first element ` `    ``queue<``int``> q; ` `    ``q.push(0); ` ` `  `    ``// Continue this until all vertices are visited ` `    ``while` `(!q.empty()) { ` `        ``int` `x = q.front(); ` ` `  `        ``// Remove the first element ` `        ``q.pop(); ` ` `  `        ``for` `(``int` `i = 0; i < gr[x].size(); i++) { ` ` `  `            ``// Check if a vertex is already visited or not ` `            ``if` `(vis[gr[x][i]] == 1) ` `                ``continue``; ` ` `  `            ``// Make vertex visited ` `            ``vis[gr[x][i]] = 1; ` ` `  `            ``// Store the number of moves to reach element ` `            ``dist[gr[x][i]] = dist[x] + 1; ` ` `  `            ``// Push the current vertex into the queue ` `            ``q.push(gr[x][i]); ` `        ``} ` `    ``} ` ` `  `    ``// Return the minimum number of ` `    ``// moves to reach (n - 1)th index ` `    ``return` `dist[n - 1]; ` `} ` ` `  `// Function to return the minimum number of moves ` `// required to reach the end of the array ` `int` `Min_Moves(``int` `a[], ``int` `n) ` `{ ` ` `  `    ``// To store the positions of each element ` `    ``vector<``int``> fre; ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``if` `(i != n - 1) ` `            ``add_edge(i, i + 1); ` ` `  `        ``fre[a[i]].push_back(i); ` `    ``} ` ` `  `    ``// Add edge between same elements ` `    ``for` `(``int` `i = 0; i < 10; i++) { ` `        ``for` `(``int` `j = 0; j < fre[i].size(); j++) { ` `            ``for` `(``int` `k = j + 1; k < fre[i].size(); k++) { ` `                ``if` `(fre[i][j] + 1 != fre[i][k] ` `                    ``and fre[i][j] - 1 != fre[i][k]) { ` `                    ``add_edge(fre[i][j], fre[i][k]); ` `                ``} ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// Return the required minimum number of moves ` `    ``return` `dijkstra(n); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `a[] = { 1, 2, 3, 4, 1, 5 }; ` `    ``int` `n = ``sizeof``(a) / ``sizeof``(a); ` ` `  `    ``cout << Min_Moves(a, n); ` ` `  `    ``return` `0; ` `} `

## Python3

 `# Python3 implementation of the approach ` `from` `collections ``import` `deque ` `N ``=` `100005` ` `  `gr ``=` `[[] ``for` `i ``in` `range``(N)] ` ` `  `# Function to add edge ` `def` `add_edge(u, v): ` `    ``gr[u].append(v) ` `    ``gr[v].append(u) ` ` `  `# Function to return the minimum path ` `# from 0th node to the (n - 1)th node ` `def` `dijkstra(n): ` `     `  `    ``# To check whether an edge is visited ` `    ``# or not and to keep distance of vertex ` `    ``# from 0th index ` `    ``vis ``=` `[``0` `for` `i ``in` `range``(n)] ` `    ``dist ``=` `[``10``*``*``9` `for` `i ``in` `range``(n)] ` ` `  `    ``# Make 0th index visited and  ` `    ``# distance is zero ` `    ``vis[``0``] ``=` `1` `    ``dist[``0``] ``=` `0` ` `  `    ``# Take a queue and  ` `    ``# append first element ` `    ``q ``=` `deque() ` `    ``q.append(``0``) ` ` `  `    ``# Continue this until   ` `    ``# all vertices are visited ` `    ``while` `(``len``(q) > ``0``): ` `        ``x ``=` `q.popleft() ` ` `  `        ``# Remove the first element ` `        ``for` `i ``in` `gr[x]: ` ` `  `            ``# Check if a vertex is  ` `            ``# already visited or not ` `            ``if` `(vis[i] ``=``=` `1``): ` `                ``continue` ` `  `            ``# Make vertex visited ` `            ``vis[i] ``=` `1` ` `  `            ``# Store the number of moves  ` `            ``# to reach element ` `            ``dist[i] ``=` `dist[x] ``+` `1` ` `  `            ``# Push the current vertex ` `            ``# into the queue ` `            ``q.append(i) ` ` `  `    ``# Return the minimum number of ` `    ``# moves to reach (n - 1)th index ` `    ``return` `dist[n ``-` `1``] ` ` `  `# Function to return the minimum number of moves ` `# required to reach the end of the array ` `def` `Min_Moves(a, n): ` ` `  `    ``# To store the positions of each element ` `    ``fre ``=` `[[] ``for` `i ``in` `range``(``10``)] ` `    ``for` `i ``in` `range``(n): ` `        ``if` `(i !``=` `n ``-` `1``): ` `            ``add_edge(i, i ``+` `1``) ` ` `  `        ``fre[a[i]].append(i) ` ` `  `    ``# Add edge between same elements ` `    ``for` `i ``in` `range``(``10``): ` `        ``for` `j ``in` `range``(``len``(fre[i])): ` `            ``for` `k ``in` `range``(j ``+` `1``,``len``(fre[i])): ` `                ``if` `(fre[i][j] ``+` `1` `!``=` `fre[i][k] ``and`  `                    ``fre[i][j] ``-` `1` `!``=` `fre[i][k]): ` `                    ``add_edge(fre[i][j], fre[i][k]) ` ` `  `    ``# Return the required  ` `    ``# minimum number of moves ` `    ``return` `dijkstra(n) ` ` `  `# Driver code ` `a ``=` `[``1``, ``2``, ``3``, ``4``, ``1``, ``5``] ` `n ``=` `len``(a) ` ` `  `print``(Min_Moves(a, n)) ` ` `  `# This code is contributed by Mohit Kumar `

Output:

```2
```

My Personal Notes arrow_drop_up pawanasipugmailcom

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.

Improved By : mohit kumar 29

Article Tags :
Practice Tags :

3

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.