Skip to content

Commit dbfd9a1

Browse files
authored
Merge pull request #313 from sir-gon/feature/minimum_swaps_2
[Hacker Rank] Interview Preparation Kit: Arrays: Minimum Swaps 2. Sol…
2 parents ce2d11b + ab5e225 commit dbfd9a1

File tree

4 files changed

+209
-0
lines changed

4 files changed

+209
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# [Arrays: New Year Chaos](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 `
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.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.md]]
3+
*/
4+
5+
package hackerrank
6+
7+
func minimumSwaps(arr []int32) int32 {
8+
var size int32 = int32(len(arr))
9+
var indexedGroup = make(map[int32]int32, size)
10+
var swaps int32 = 0
11+
var index int32 = 0
12+
13+
for key, value := range arr {
14+
indexedGroup[int32(key)] = value - 1
15+
}
16+
17+
for index < size {
18+
if indexedGroup[index] == index {
19+
index += 1
20+
} else {
21+
var temp = indexedGroup[index]
22+
indexedGroup[index] = indexedGroup[temp]
23+
indexedGroup[temp] = temp
24+
swaps += 1
25+
}
26+
}
27+
28+
return swaps
29+
}
30+
31+
func MinimumSwaps(arr []int32) int32 {
32+
return minimumSwaps(arr)
33+
}
Lines changed: 5 additions & 0 deletions
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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package hackerrank
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"gon.cl/algorithms/utils"
10+
)
11+
12+
type MinimumTwoSwaps2TestCase struct {
13+
Input []int32 `json:"input"`
14+
Expected int32 `json:"expected"`
15+
}
16+
17+
var MinimumTwoSwaps2testCases []MinimumTwoSwaps2TestCase
18+
19+
// You can use testing.T, if you want to test the code without benchmarking
20+
func testMinimumTwoSwaps2SetupSuite(t testing.TB) {
21+
wd, _ := os.Getwd()
22+
filepath := wd + "/minimum_swaps_2.testcases.json"
23+
t.Log("Setup test cases from JSON: ", filepath)
24+
25+
var _, err = utils.LoadJSON(filepath, &MinimumTwoSwaps2testCases)
26+
if err != nil {
27+
t.Log(err)
28+
}
29+
}
30+
31+
func TestMinimumTwoSwaps2(t *testing.T) {
32+
33+
testMinimumTwoSwaps2SetupSuite(t)
34+
35+
for _, tt := range MinimumTwoSwaps2testCases {
36+
testname := fmt.Sprintf("MinimumSwaps(%v) => %d \n", tt.Input, tt.Expected)
37+
t.Run(testname, func(t *testing.T) {
38+
ans := MinimumSwaps(tt.Input)
39+
assert.Equal(t, tt.Expected, ans)
40+
})
41+
42+
}
43+
}

0 commit comments

Comments
 (0)