Skip to content

[Hacker Rank] Interview Preparation Kit: Arrays: Minimum Swaps 2. Sol… #223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ae.hackerrank.interview_preparation_kit.arrays;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;


/**
* Crush (Optimized).
*
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.md]]
*/
public class MinimumSwaps2 {

private MinimumSwaps2() {}

/**
* minimumSwaps.
*/
static int minimumSwaps(int[] arr) {
List<Integer> indexedGroup = IntStream.of(arr)
.boxed()
.map(t -> t - 1)
.collect(Collectors.toList());

int swaps = 0;
int index = 0;
int size = indexedGroup.size();

while (index < size) {
if (indexedGroup.get(index) == index) {
index += 1;
} else {
int temp = indexedGroup.get(index);
indexedGroup.set(index, indexedGroup.get(temp));
indexedGroup.set(temp, temp);
swaps += 1;
}
}
return swaps;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ae.hackerrank.interview_preparation_kit.arrays;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;
import java.util.List;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import util.JsonLoader;


@TestInstance(Lifecycle.PER_CLASS)
class MinimumSwaps2Test {

public static class MinimumSwaps2TestCase {
public String title;
public List<Integer> input;
public long expected;
}

List<MinimumSwaps2TestCase> testCases;

@BeforeAll
public void setup() throws IOException {
String path = String.join("/", "hackerrank",
"interview_preparation_kit",
"arrays",
"minimum_swaps_2.testcases.json");

this.testCases = JsonLoader.loadJson(path, MinimumSwaps2TestCase.class);
}

@Test void testArrayManipulation() {
for (MinimumSwaps2TestCase testCase : testCases) {
int[] input = testCase.input
.stream()
.mapToInt(Integer::intValue)
.toArray();
long solutionFound = MinimumSwaps2.minimumSwaps(input);

assertEquals(testCase.expected, solutionFound,
"%s(%s) answer must be: %s".formatted(
"MinimumSwaps2.minimumSwaps",
testCase.input.toString(),
testCase.expected
)
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{"title": "Sample input 0", "input": [4, 3, 1, 2], "expected": 3},
{"title": "Sample input 1", "input": [2, 3, 4, 1, 5], "expected": 3},
{"title": "Sample input 2", "input": [1, 3, 5, 2, 4, 6, 7], "expected": 3}
]
128 changes: 128 additions & 0 deletions docs/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# [Arrays: Minimum Swaps 2](https://www.hackerrank.com/challenges/minimum-swaps-2)

Return the minimum number of swaps to sort the given array.

- Difficulty: `#medium`
- Category: `#ProblemSolvingIntermediate` `#arrays`

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:

```text
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, , the size of .
The second line contains space-separated integers .

## Constraints

Sample Input 0

- $ 1 \leq n \leq 10^5 $
- $ 1 \leq arr[i] \leq 10^5 $

## Sample Input 0

```text
4
4 3 1 2
```

## Sample Output 0

```text
3
```

## Explanation 0

Given array `arr: [4, 3, 1, 2]`

After swapping `(0, 2)` we get `[1, 3, 4, 2]`

After swapping `(1, 2)` we get `[1, 4, 3, 2]`

After swapping `(1, 3)` we get `[1, 2, 3, 4]`

So, we need a minimum of `3` swaps to sort the array in ascending order.

## Sample Input 1

```text
5
2 3 4 1 5
```

## Sample Output 1

```text
3
```

## Explanation 1

Given array `arr: [1, 3, 5, 2, 4, 6, 7]`

After swapping `(1, 3)` we get `[2, 3, 1, 4, 5]`

After swapping `(0, 1)` we get `[3, 2, 1, 4, 5]`

After swapping `(0, 2)` we get `[1, 2, 3, 4, 5]`

So, we need a minimum of `3` swaps to sort the array in ascending order.

## Sample Input 2

```text
7
1 3 5 2 4 6 7
```

## Sample Output 2

```text
3
```

## Explanation 2

Given array `[1, 3, 5, 2, 4, 6, 7]`

After swapping `(1, 3)` we get `[1, 2, 5, 3, 4, 6, 7]`

After swapping `(2, 3)` we get `[1, 2, 3, 5, 4, 6, 7]`

After swapping `(3, 4)` we get `[1, 2, 3, 4, 5, 6, 7]`

So, we need a minimum of `3` swaps to sort the array in ascending order.