Minimum Swaps 2
Problem Statement :
You are given an unordered array consisting of consecutive integers [1, 2, 3, ..., n] without any duplicates. You are allowed to swap any two elements. Find the minimum number of swaps required to sort the array in ascending order. Example arr = [ 7 , 1 , 3 , 2, 4 , 5, 6 ] Perform the following steps: i arr swap (indices) 0 [7, 1, 3, 2, 4, 5, 6] swap (0,3) 1 [2, 1, 3, 7, 4, 5, 6] swap (0,1) 2 [1, 2, 3, 7, 4, 5, 6] swap (3,4) 3 [1, 2, 3, 4, 7, 5, 6] swap (4,5) 4 [1, 2, 3, 4, 5, 7, 6] swap (5,6) 5 [1, 2, 3, 4, 5, 6, 7] It took 5 swaps to sort the array. Function Description Complete the function minimumSwaps in the editor below. minimumSwaps has the following parameter(s): int arr[n]: an unordered array of integers Returns int: the minimum number of swaps to sort the array Input Format The first line contains an integer, n, the size of arr . The second line contains n space-separated integers arr[ i ] . Constraints 1 <= n <= 10^5 1 <= arr[i] <= n Sample Input 0 4 4 3 1 2 Sample Output 0 3
Solution :
Solution in C++ :
In C++ :
vector<int>v[100003];
bool visit[100003];
int dfs(int i)
{
visit[i] = true;
int z = 1;
for(auto x: v[i])
if(!visit[x])
z += dfs(x);
return z;
}
int minimumSwaps(vector<int> A) {
for(int i = 0; i < A.size(); ++i )
v[i].push_back(A[i]-1), v[A[i]-1].push_back(i);
int c = 0;
for(int i = 0; i < A.size(); ++i)
{
if(!visit[i])
c += dfs(i) - 1;
}
return c;
}
Solution in Java :
In Java :
static int minimumSwaps(int[] a) {
int swap=0;
for(int i=0;i<a.length;i++){
if(i+1!=a[i]){
int t=i;
while(a[t]!=i+1){
t++;
}
int temp=a[t];
a[t]=a[i];
a[i]=temp;
swap++;
}
}
return swap;
}
Solution in Python :
In Python3 :
def minimumSwaps(arr):
ref_arr = sorted(arr)
index_dict = {v: i for i,v in enumerate(arr)}
swaps = 0
for i,v in enumerate(arr):
correct_value = ref_arr[i]
if v != correct_value:
to_swap_ix = index_dict[correct_value]
arr[to_swap_ix],arr[i] = arr[i], arr[to_swap_ix]
index_dict[v] = to_swap_ix
index_dict[correct_value] = i
swaps += 1
return swaps
View More Similar Problems
Lazy White Falcon
White Falcon just solved the data structure problem below using heavy-light decomposition. Can you help her find a new solution that doesn't require implementing any fancy techniques? There are 2 types of query operations that can be performed on a tree: 1 u x: Assign x as the value of node u. 2 u v: Print the sum of the node values in the unique path from node u to node v. Given a tree wi
View Solution →Ticket to Ride
Simon received the board game Ticket to Ride as a birthday present. After playing it with his friends, he decides to come up with a strategy for the game. There are n cities on the map and n - 1 road plans. Each road plan consists of the following: Two cities which can be directly connected by a road. The length of the proposed road. The entire road plan is designed in such a way that if o
View Solution →Heavy Light White Falcon
Our lazy white falcon finally decided to learn heavy-light decomposition. Her teacher gave an assignment for her to practice this new technique. Please help her by solving this problem. You are given a tree with N nodes and each node's value is initially 0. The problem asks you to operate the following two types of queries: "1 u x" assign x to the value of the node . "2 u v" print the maxim
View Solution →Number Game on a Tree
Andy and Lily love playing games with numbers and trees. Today they have a tree consisting of n nodes and n -1 edges. Each edge i has an integer weight, wi. Before the game starts, Andy chooses an unordered pair of distinct nodes, ( u , v ), and uses all the edge weights present on the unique path from node u to node v to construct a list of numbers. For example, in the diagram below, Andy
View Solution →Heavy Light 2 White Falcon
White Falcon was amazed by what she can do with heavy-light decomposition on trees. As a resut, she wants to improve her expertise on heavy-light decomposition. Her teacher gave her an another assignment which requires path updates. As always, White Falcon needs your help with the assignment. You are given a tree with N nodes and each node's value Vi is initially 0. Let's denote the path fr
View Solution →Library Query
A giant library has just been inaugurated this week. It can be modeled as a sequence of N consecutive shelves with each shelf having some number of books. Now, being the geek that you are, you thought of the following two queries which can be performed on these shelves. Change the number of books in one of the shelves. Obtain the number of books on the shelf having the kth rank within the ra
View Solution →