# Check if the array can be sorted using swaps between given indices only

Given an array arr[] of size N consisting of distinct integers from range [0, N – 1] arranged in a random order. Also given a few pairs where each pair denotes the indices where the elements of the array can be swapped. There is no limit on the number of swaps allowed. The task is to find if it is possible to arrange the array in ascending order using these swaps. If possible then print Yes else print No.

Examples:

Input: arr[] = {0, 4, 3, 2, 1, 5}, pairs[][] = {{1, 4}, {2, 3}}
Output: Yes
swap(arr[1], arr[4]) -> arr[] = {0, 1, 3, 2, 4, 5}
swap(arr[2], arr[3]) -> arr[] = {0, 1, 2, 3, 4, 5}

Input: arr[] = {1, 2, 3, 0, 4}, pairs[][] = {{2, 3}}
Output: No

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

Approach: The given problem can be considered as a graph problem where N denotes the total number of nodes in the graph and each swapping pair denotes an undirected edge in the graph. We have to find out if it is possible to convert the input array in the form of {0, 1, 2, 3, …, N – 1}.
Let us call the above array as B. Now find out all the connected components of both the arrays and if the elements differ for at least one component then the answer is No else the answer is Yes.

Below is the implementation of the above approach:

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function that returns true if the array elements ` `// can be sorted with the given operation ` `bool` `canBeSorted(``int` `N, vector<``int``> a, ``int` `P,  ` `vector > vp) ` `{ ` ` `  `    ``// To create the adjacency list of the graph ` `    ``vector<``int``> v[N]; ` ` `  `    ``// Boolean array to mark the visited nodes ` `    ``bool` `vis[N] = { ``false` `}; ` ` `  `    ``// Creating adjacency list for undirected graph ` `    ``for` `(``int` `i = 0; i < P; i++) { ` `        ``v[vp[i].first].push_back(vp[i].second); ` `        ``v[vp[i].second].push_back(vp[i].first); ` `    ``} ` ` `  `    ``for` `(``int` `i = 0; i < N; i++) { ` ` `  `        ``// If not already visited ` `        ``// then apply BFS ` `        ``if` `(!vis[i]) { ` `            ``queue<``int``> q; ` `            ``vector<``int``> v1; ` `            ``vector<``int``> v2; ` ` `  `            ``// Set visited to true ` `            ``vis[i] = ``true``; ` ` `  `            ``// Push the node to the queue ` `            ``q.push(i); ` ` `  `            ``// While queue is not empty ` `            ``while` `(!q.empty()) { ` `                ``int` `u = q.front(); ` `                ``v1.push_back(u); ` `                ``v2.push_back(a[u]); ` `                ``q.pop(); ` ` `  `                ``// Check all the adjacent nodes ` `                ``for` `(``auto` `s : v[u]) { ` ` `  `                    ``// If not visited ` `                    ``if` `(!vis[s]) { ` ` `  `                        ``// Set visited to true ` `                        ``vis[s] = ``true``; ` `                        ``q.push(s); ` `                    ``} ` `                ``} ` `            ``} ` `            ``sort(v1.begin(), v1.end()); ` `            ``sort(v2.begin(), v2.end()); ` ` `  `            ``// If the connected component does not ` `            ``// contain same elements then return false ` `            ``if` `(v1 != v2) ` `                ``return` `false``; ` `        ``} ` `    ``} ` `    ``return` `true``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``vector<``int``> a = { 0, 4, 3, 2, 1, 5 }; ` `    ``int` `n = a.size(); ` `    ``vector > vp = { { 1, 4 }, { 2, 3 } }; ` `    ``int` `p = vp.size(); ` ` `  `    ``if` `(canBeSorted(n, a, p, vp)) ` `        ``cout << ``"Yes"``; ` `    ``else` `        ``cout << ``"No"``; ` ` `  `    ``return` `0; ` `} `

Output:

```Yes
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.