`{"id":"6d18622c-bf9e-4846-babf-cb6eb5efb3c9","name":"Minimum Number Of Swaps Required To Sort An Array","description":"Given an array of n distinct elements, find the minimum number of swaps required to sort the array.","inputFormat":"First line contains an integer N . \r\nSecond line has 2 space separated integers ai and bi.","outputFormat":"Print in one line the minimum number of swaps required to sort the given array.","constraints":"1 &lt;= n &lt;= 1000000\r\n1 &lt;= arr[i] &lt;= 1000000000","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\n\n\nint minSwaps(vector<int> &arr1)\n{\n\n //write code here\n \n}\n\nint main()\n{\n int n;\n cin>>n;\n vector<int> arr(n);\n for(int i=0;i<n;i++)\n {\n cin>>arr[i];\n }\n \n cout<<minSwaps(arr);\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n int n = Integer.parseInt(br.readLine());\r\n int[] arr = new int[n];\r\n\r\n String[] st = br.readLine().split(\" \");\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = Integer.parseInt(st[i]);\r\n }\r\n\r\n System.out.println(minSwaps(arr));\r\n }\r\n\r\n public static int minSwaps(int[] arr1) {\r\n\r\n }\r\n\r\n private static class Pair implements Comparable<Pair> {\r\n int val;\r\n int idx;\r\n\r\n Pair(int val, int idx) {\r\n this.val = val;\r\n this.idx = idx;\r\n }\r\n\r\n @Override\r\n public int compareTo(Pair o) {\r\n return this.val - o.val;\r\n }\r\n }\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"5\r\n10 19 6 3 5","sampleOutput":"2\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"7e07fddf-83bd-421e-848f-118f1f29541c","name":"Graphs For Intermediate","slug":"graphs-for-intermediate-493","type":0},{"id":"5fcd6bad-34c3-4389-81aa-fda4f88ad9ea","name":"Minimum Number Of Swaps Required To Sort An Array","slug":"minimum-number-of-swaps-required-to-sort-an-array","type":1}],"next":{"id":"d94da9a0-5d70-4095-985f-226e33ec7fd9","name":"Minimum Number of Swaps Required to Sort an Array","type":0,"slug":"minimum-number-of-swaps-required-to-sort-an-array"},"prev":{"id":"4c8eb08b-ae80-493e-a025-17b608794999","name":"Bus Routes","type":3,"slug":"bus-routes"}}}`

# Minimum Number Of Swaps Required To Sort An Array

Given an array of n distinct elements, find the minimum number of swaps required to sort the array.

`{"id":"6d18622c-bf9e-4846-babf-cb6eb5efb3c9","name":"Minimum Number Of Swaps Required To Sort An Array","description":"Given an array of n distinct elements, find the minimum number of swaps required to sort the array.","inputFormat":"First line contains an integer N . \r\nSecond line has 2 space separated integers ai and bi.","outputFormat":"Print in one line the minimum number of swaps required to sort the given array.","constraints":"1 &lt;= n &lt;= 1000000\r\n1 &lt;= arr[i] &lt;= 1000000000","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\n\n\nint minSwaps(vector<int> &arr1)\n{\n\n //write code here\n \n}\n\nint main()\n{\n int n;\n cin>>n;\n vector<int> arr(n);\n for(int i=0;i<n;i++)\n {\n cin>>arr[i];\n }\n \n cout<<minSwaps(arr);\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n int n = Integer.parseInt(br.readLine());\r\n int[] arr = new int[n];\r\n\r\n String[] st = br.readLine().split(\" \");\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = Integer.parseInt(st[i]);\r\n }\r\n\r\n System.out.println(minSwaps(arr));\r\n }\r\n\r\n public static int minSwaps(int[] arr1) {\r\n\r\n }\r\n\r\n private static class Pair implements Comparable<Pair> {\r\n int val;\r\n int idx;\r\n\r\n Pair(int val, int idx) {\r\n this.val = val;\r\n this.idx = idx;\r\n }\r\n\r\n @Override\r\n public int compareTo(Pair o) {\r\n return this.val - o.val;\r\n }\r\n }\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"5\r\n10 19 6 3 5","sampleOutput":"2\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"7e07fddf-83bd-421e-848f-118f1f29541c","name":"Graphs For Intermediate","slug":"graphs-for-intermediate-493","type":0},{"id":"5fcd6bad-34c3-4389-81aa-fda4f88ad9ea","name":"Minimum Number Of Swaps Required To Sort An Array","slug":"minimum-number-of-swaps-required-to-sort-an-array","type":1}],"next":{"id":"d94da9a0-5d70-4095-985f-226e33ec7fd9","name":"Minimum Number of Swaps Required to Sort an Array","type":0,"slug":"minimum-number-of-swaps-required-to-sort-an-array"},"prev":{"id":"4c8eb08b-ae80-493e-a025-17b608794999","name":"Bus Routes","type":3,"slug":"bus-routes"}}}` Editor

# Minimum Number Of Swaps Required To Sort An Array

easy

Given an array of n distinct elements, find the minimum number of swaps required to sort the array.

## Constraints

1 <= n <= 1000000 1 <= arr[i] <= 1000000000

## Format

### Input

First line contains an integer N . Second line has 2 space separated integers ai and bi.

### Output

Print in one line the minimum number of swaps required to sort the given array.

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}5 10 19 6 3 5```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}2 ```

Discussions

Show Discussion

Related Resources 