forked from keshavagarwal17/All-Cp-Algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswap.java
37 lines (35 loc) · 789 Bytes
/
swap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package development;
import java.util.*;
public class swap {
public static void main(String[] args) {
// TODO Auto-generated method stub
int count=0;
Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
int []arr = new int[a];
int []arr1 = new int[a];
for(int i=0;i<a;i++) {
arr[i] = scan.nextInt();
arr1[i] = arr[i];
}
Arrays.sort(arr1);
HashMap<Integer, Integer> h
= new HashMap<Integer, Integer>();
for(int i=0;i<a;i++) {
h.put(arr[i], i);
}
for(int i=0;i<a;i++) {
if(arr[i]!=arr1[i]) {
count++;
int t = h.get(arr1[i]);
int z = arr1[i];
int temp = arr[i];
arr[i] = arr[t];
arr[t] = temp;
h.put(z,i);
h.put(temp,t);
}
}
System.out.println(count);
}
}