Skip to content

Commit ee4377c

Browse files
authored
Merge pull request #388 from sir-gon/feature/minimum-swaps-2
[Hacker Rank] Interview Preparation Kit: Arrays: Minimum Swaps 2. Sol…
2 parents 16a771d + 224bdce commit ee4377c

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
import { logger as console } from '../../../logger';
3+
4+
import { minimumSwaps } from './minimum_swaps_2';
5+
6+
const TEST_CASES = [
7+
{ title: 'Sample input 0', input: [4, 3, 1, 2], expected: 3 },
8+
{ title: 'Sample input 1', input: [2, 3, 4, 1, 5], expected: 3 },
9+
{ title: 'Sample input 2', input: [1, 3, 5, 2, 4, 6, 7], expected: 3 }
10+
];
11+
12+
describe('minimum swaps 2', () => {
13+
it('minimumSwaps', () => {
14+
expect.assertions(3);
15+
16+
TEST_CASES.forEach((test) => {
17+
const answer = minimumSwaps(test.input);
18+
19+
console.debug(`minimumSwaps(${test.input}) solution found: ${answer}`);
20+
21+
expect(answer).toStrictEqual(test.expected);
22+
});
23+
});
24+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.md]]
3+
*/
4+
5+
export function minimumSwaps(arr: number[]): number {
6+
const indexed_group = arr.map((x) => x - 1);
7+
let swaps = 0;
8+
let index = 0;
9+
const size = indexed_group.length;
10+
11+
while (index < size) {
12+
if (indexed_group[index] == index) {
13+
index += 1;
14+
} else {
15+
const temp = indexed_group[index];
16+
indexed_group[index] = indexed_group[temp];
17+
indexed_group[temp] = temp;
18+
swaps += 1;
19+
}
20+
}
21+
22+
return swaps;
23+
}
24+
25+
export default { minimumSwaps };

0 commit comments

Comments
 (0)