diff --git a/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/arrays/MinimumSwaps2.java b/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/arrays/MinimumSwaps2.java new file mode 100644 index 0000000..8d67b92 --- /dev/null +++ b/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/arrays/MinimumSwaps2.java @@ -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 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; + } +} diff --git a/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/arrays/MinimumSwaps2Test.java b/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/arrays/MinimumSwaps2Test.java new file mode 100644 index 0000000..640a757 --- /dev/null +++ b/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/arrays/MinimumSwaps2Test.java @@ -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 input; + public long expected; + } + + List 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 + ) + ); + } + } +} diff --git a/algorithm-exercises-java/src/test/resources/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.testcases.json b/algorithm-exercises-java/src/test/resources/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.testcases.json new file mode 100644 index 0000000..6a81402 --- /dev/null +++ b/algorithm-exercises-java/src/test/resources/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.testcases.json @@ -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} +] diff --git a/docs/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.md b/docs/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.md new file mode 100644 index 0000000..01fceb9 --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.md @@ -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.