Skip to content

Commit 86514c3

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Interview Preparation Kit: Arrays: Minimum Swaps 2. Solved ✅.
1 parent d911a2b commit 86514c3

File tree

4 files changed

+229
-0
lines changed

4 files changed

+229
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package ae.hackerrank.interview_preparation_kit.arrays;
2+
3+
import java.util.List;
4+
import java.util.stream.Collectors;
5+
import java.util.stream.IntStream;
6+
7+
8+
/**
9+
* Crush (Optimized).
10+
*
11+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.md]]
12+
*/
13+
public class MinimumSwaps2 {
14+
15+
private MinimumSwaps2() {}
16+
17+
/**
18+
* minimumSwaps.
19+
*/
20+
static int minimumSwaps(int[] arr) {
21+
List<Integer> indexedGroup = IntStream.of(arr)
22+
.boxed()
23+
.map(t -> t - 1)
24+
.collect(Collectors.toList());
25+
26+
int swaps = 0;
27+
int index = 0;
28+
int size = indexedGroup.size();
29+
30+
while (index < size) {
31+
32+
if (indexedGroup.get(index) == index) {
33+
index += 1;
34+
} else {
35+
int temp = indexedGroup.get(index);
36+
indexedGroup.set(index, indexedGroup.get(temp));
37+
indexedGroup.set(temp, temp);
38+
swaps += 1;
39+
}
40+
41+
}
42+
return swaps;
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package ae.hackerrank.interview_preparation_kit.arrays;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.io.IOException;
6+
import java.util.List;
7+
import org.junit.jupiter.api.BeforeAll;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.TestInstance;
10+
import org.junit.jupiter.api.TestInstance.Lifecycle;
11+
import util.JsonLoader;
12+
13+
14+
@TestInstance(Lifecycle.PER_CLASS)
15+
class MinimumSwaps2Test {
16+
17+
public static class MinimumSwaps2TestCase {
18+
public String title;
19+
public List<Integer> input;
20+
public long expected;
21+
}
22+
23+
List<MinimumSwaps2TestCase> testCases;
24+
25+
@BeforeAll
26+
public void setup() throws IOException {
27+
String path = String.join("/", "hackerrank",
28+
"interview_preparation_kit",
29+
"arrays",
30+
"minimum_swaps_2.testcases.json");
31+
32+
this.testCases = JsonLoader.loadJson(path, MinimumSwaps2TestCase.class);
33+
}
34+
35+
@Test void testArrayManipulation() {
36+
for (MinimumSwaps2TestCase testCase : testCases) {
37+
int[] input = testCase.input
38+
.stream()
39+
.mapToInt(Integer::intValue)
40+
.toArray();
41+
long solutionFound = MinimumSwaps2.minimumSwaps(input);
42+
43+
assertEquals(testCase.expected, solutionFound,
44+
"%s(%s) answer must be: %s".formatted(
45+
"MinimumSwaps2.minimumSwaps",
46+
testCase.input.toString(),
47+
testCase.expected
48+
)
49+
);
50+
}
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
{"title": "Sample input 0", "input": [4, 3, 1, 2], "expected": 3},
3+
{"title": "Sample input 1", "input": [2, 3, 4, 1, 5], "expected": 3},
4+
{"title": "Sample input 2", "input": [1, 3, 5, 2, 4, 6, 7], "expected": 3}
5+
]
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# [Arrays: Minimum Swaps 2](https://www.hackerrank.com/challenges/minimum-swaps-2)
2+
3+
Return the minimum number of swaps to sort the given array.
4+
5+
- Difficulty: `#medium`
6+
- Category: `#ProblemSolvingIntermediate` `#arrays`
7+
8+
You are given an unordered array consisting of consecutive integers
9+
[1, 2, 3, ..., n] without any duplicates. You are allowed to swap any
10+
two elements. Find the minimum number of swaps required to sort the
11+
array in ascending order.
12+
13+
## Example
14+
15+
` arr = [7, 1, 3, 2, 4, 5, 6] `
16+
17+
Perform the following steps:
18+
19+
```text
20+
i arr swap (indices)
21+
0 [7, 1, 3, 2, 4, 5, 6] swap (0,3)
22+
1 [2, 1, 3, 7, 4, 5, 6] swap (0,1)
23+
2 [1, 2, 3, 7, 4, 5, 6] swap (3,4)
24+
3 [1, 2, 3, 4, 7, 5, 6] swap (4,5)
25+
4 [1, 2, 3, 4, 5, 7, 6] swap (5,6)
26+
5 [1, 2, 3, 4, 5, 6, 7]
27+
```
28+
29+
It took `5` swaps to sort the array.
30+
31+
## Function Description
32+
33+
Complete the function minimumSwaps in the editor below.
34+
35+
minimumSwaps has the following parameter(s):
36+
37+
- `int arr[n]`: an unordered array of integers
38+
39+
## Returns
40+
41+
- `int`: the minimum number of swaps to sort the array
42+
43+
## Input Format
44+
45+
The first line contains an integer, , the size of .
46+
The second line contains space-separated integers .
47+
48+
## Constraints
49+
50+
Sample Input 0
51+
52+
- $ 1 \leq n \leq 10^5 $
53+
- $ 1 \leq arr[i] \leq 10^5 $
54+
55+
## Sample Input 0
56+
57+
```text
58+
4
59+
4 3 1 2
60+
```
61+
62+
## Sample Output 0
63+
64+
```text
65+
3
66+
```
67+
68+
## Explanation 0
69+
70+
Given array `arr: [4, 3, 1, 2]`
71+
72+
After swapping `(0, 2)` we get `[1, 3, 4, 2]`
73+
74+
After swapping `(1, 2)` we get `[1, 4, 3, 2]`
75+
76+
After swapping `(1, 3)` we get `[1, 2, 3, 4]`
77+
78+
So, we need a minimum of `3` swaps to sort the array in ascending order.
79+
80+
## Sample Input 1
81+
82+
```text
83+
5
84+
2 3 4 1 5
85+
```
86+
87+
## Sample Output 1
88+
89+
```text
90+
3
91+
```
92+
93+
## Explanation 1
94+
95+
Given array `arr: [1, 3, 5, 2, 4, 6, 7]`
96+
97+
After swapping `(1, 3)` we get `[2, 3, 1, 4, 5]`
98+
99+
After swapping `(0, 1)` we get `[3, 2, 1, 4, 5]`
100+
101+
After swapping `(0, 2)` we get `[1, 2, 3, 4, 5]`
102+
103+
So, we need a minimum of `3` swaps to sort the array in ascending order.
104+
105+
## Sample Input 2
106+
107+
```text
108+
7
109+
1 3 5 2 4 6 7
110+
```
111+
112+
## Sample Output 2
113+
114+
```text
115+
3
116+
```
117+
118+
## Explanation 2
119+
120+
Given array `[1, 3, 5, 2, 4, 6, 7]`
121+
122+
After swapping `(1, 3)` we get `[1, 2, 5, 3, 4, 6, 7]`
123+
124+
After swapping `(2, 3)` we get `[1, 2, 3, 5, 4, 6, 7]`
125+
126+
After swapping `(3, 4)` we get `[1, 2, 3, 4, 5, 6, 7]`
127+
128+
So, we need a minimum of `3` swaps to sort the array in ascending order.

0 commit comments

Comments
 (0)