diff --git a/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/arrays/NewYearChaos.java b/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/arrays/NewYearChaos.java new file mode 100644 index 0000000..f966bc3 --- /dev/null +++ b/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/arrays/NewYearChaos.java @@ -0,0 +1,65 @@ +package ae.hackerrank.interview_preparation_kit.arrays; + +import java.util.List; + + +/** + * New Year Chaos. + * + * @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/new_year_chaos.md]] + */ +public class NewYearChaos { + + private NewYearChaos() {} + + static final String TOO_CHAOTIC_ERROR = "Too chaotic"; + + /** + * minimumBribesCalculate. + */ + @SuppressWarnings("java:S6885") + public static Integer minimumBribesCalculate(List q) { + int bribes = 0; + int i = 0; + + for (Integer value : q) { + int position = i + 1; + + if (value - position > 2) { + throw new IllegalStateException(TOO_CHAOTIC_ERROR); + } + + List fragment = q.subList(Math.min(Math.max(value - 2, 0), i), i); + + for (Integer k : fragment) { + if (k > value) { + bribes += 1; + } + } + i += 1; + } + + return bribes; + } + + /** + * minimumBribes. + */ + public static String minimumBribesText(List q) { + try { + Integer bribes = minimumBribesCalculate(q); + return String.format("%d", bribes); + } catch (IllegalStateException e) { + return String.format(e.getMessage()); + } + } + + /** + * minimumBribesText. + */ + @SuppressWarnings("java:S106") + public static void minimumBribes(List q) { + System.out.println(minimumBribesText(q)); + } + +} diff --git a/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/arrays/NewYearChaosTest.java b/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/arrays/NewYearChaosTest.java new file mode 100644 index 0000000..534c8dc --- /dev/null +++ b/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/arrays/NewYearChaosTest.java @@ -0,0 +1,51 @@ +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 NewYearChaosTest { + + public static class NewYearChaosTestCase { + public String title; + public List input; + public String expected; + } + + List testCases; + + @BeforeAll + public void setup() throws IOException { + + String path = String.join("/", "hackerrank", + "interview_preparation_kit", + "arrays", + "new_year_chaos.testcases.json"); + + this.testCases = JsonLoader.loadJson(path, NewYearChaosTestCase.class); + } + + @Test void testMinimumBribesText() { + + for (NewYearChaosTestCase test : this.testCases) { + String solutionFound = NewYearChaos.minimumBribesText(test.input); + NewYearChaos.minimumBribes(test.input); + + assertEquals(test.expected, solutionFound, + "%s(%s) answer must be: %s".formatted( + "NewYearChaosTestCase.minimumBribesText", + test.input, + test.expected + ) + ); + } + } + +} diff --git a/algorithm-exercises-java/src/test/resources/hackerrank/interview_preparation_kit/arrays/new_year_chaos.testcases.json b/algorithm-exercises-java/src/test/resources/hackerrank/interview_preparation_kit/arrays/new_year_chaos.testcases.json new file mode 100644 index 0000000..5527c31 --- /dev/null +++ b/algorithm-exercises-java/src/test/resources/hackerrank/interview_preparation_kit/arrays/new_year_chaos.testcases.json @@ -0,0 +1,27 @@ +[ + { + "title": "Test Case 0-0", + "input": [2, 1, 5, 3, 4], + "expected": 3 + }, + { + "title": "Test Case 0-1", + "input": [2, 5, 1, 3, 4], + "expected": "Too chaotic" + }, + { + "title": "Test Case 1-1", + "input": [5, 1, 2, 3, 7, 8, 6, 4], + "expected": "Too chaotic" + }, + { + "title": "Test Case 1-2", + "input": [1, 2, 5, 3, 7, 8, 6, 4], + "expected": 7 + }, + { + "title": "Test Case 2", + "input": [1, 2, 5, 3, 4, 7, 8, 6], + "expected": 4 + } + ] diff --git a/docs/hackerrank/interview_preparation_kit/arrays/new_year_chaos-solution-notes.md b/docs/hackerrank/interview_preparation_kit/arrays/new_year_chaos-solution-notes.md new file mode 100644 index 0000000..e67bd4d --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/arrays/new_year_chaos-solution-notes.md @@ -0,0 +1,16 @@ +# [Arrays: New Year Chaos](https://www.hackerrank.com/challenges/new-year-chaos) + +Determine how many bribes took place to get a queue into its current state. + +- Difficulty: `#medium` +- Category: `#ProblemSolvingBasic` `#arrays` + +## Solution sources + +- This solution focuses on "who were bribed" and counts his displacements with +respect to those who bribed him +(they have original position numbers greater than this one in front of him): +[Solution to HackerRank's New Year Chaos in Python](https://csanim.com/tutorials/hackerrank-solution-new-year-chaos) + +- This solution focuses on the expected positions and compares whether the "briber" + is ahead in line: diff --git a/docs/hackerrank/interview_preparation_kit/arrays/new_year_chaos.md b/docs/hackerrank/interview_preparation_kit/arrays/new_year_chaos.md new file mode 100644 index 0000000..aa96b3b --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/arrays/new_year_chaos.md @@ -0,0 +1,182 @@ +# [Arrays: New Year Chaos](https://www.hackerrank.com/challenges/new-year-chaos) + +Determine how many bribes took place to get a queue into its current state. + +- Difficulty: `#medium` +- Category: `#ProblemSolvingBasic` `#arrays` + +It is New Year's Day and people are in line for the Wonderland rollercoaster ride. +Each person wears a sticker indicating their initial position in +the queue from ` 1 ` to ` n `. + +Any person can bribe the person directly in front of them to swap positions, +but they still wear their original sticker. One person can bribe at most two others. + +Determine the minimum number of bribes that took place to get +to a given queue order. +Print the number of bribes, or, if anyone has bribed more than two people, +print ` Too chaotic `. + +## Example + +$ q = [1, 2, 3, 4, 5, 6, 7, 8] $ + +If person ` 5 ` bribes person ` 4 `, the queue will look like this: +$ [1, 2, 3, 5, 4, 6, 7, 8] $. Only ` 1 ` bribe is required. Print ` 1 `. + +$ q = [4, 1, 2, 3] $ + +Person ` 4 ` had to bribe ` 3 ` people to get to the current position. +Print `Too chaotic`. + +## Function Description + +Complete the function minimumBribes in the editor below. + +minimumBribes has the following parameter(s): + +- `int q[n]`: the positions of the people after all bribes + +## Returns + +- No value is returned. Print the minimum number of bribes necessary or + ` Too chaotic ` if someone has bribed more than people. + +## Input Format + +The first line contains an integer ` t `, the number of test cases. + +Each of the next ` t ` pairs of lines are as follows: + +- The first line contains an integer ` t `, the number of people in the queue +- The second line has `n` space-separated integers describing the +final state of the queue. + +## Constraints + +- $ 1 \leq t \leq 10 $ +- $ 1 \leq n \leq 10^5 $ + +## Subtasks + +For `60%` score $ 1 \leq t \leq 10^3 $ +For `100%` score $ 1 \leq t \leq 10^5 $ + +## Sample Input + +```text +STDIN Function +----- -------- +2 t = 2 +5 n = 5 +2 1 5 3 4 q = [2, 1, 5, 3, 4] +5 n = 5 +2 5 1 3 4 q = [2, 5, 1, 3, 4] +``` + +## Sample Output + +```text +3 +Too chaotic +``` + +## Explanation + +### Test Case 1 + +The initial state: + +```mermaid +flowchart LR + + A[Ride!]:::first + 1([1]) + 2([2]) + 3([3]) + 4([4]) + 5([5]) + + A ~~~ 1 + 1 -.- 2 + 2 -.- 3 + 3 -.- 4 + 4 -.- 5 + + classDef default color:#000,fill:#FFE195,stroke:#333,stroke-width:4px; + classDef first fill:#9FBCE8 + classDef emphasys fill:#FEB130 +``` + +After person `5` moves one position ahead by bribing person `4`: + +```mermaid +flowchart LR + A[Ride!]:::first + 1([1]) + 2([2]) + 3([3]) + 4([4]):::emphasys + 5([5]):::emphasys + + A ~~~ 1 + 1 -.- 2 + 2 -.- 3 + 3 -.- 5 + 5 -.- 4 + + classDef default color:#000,fill:#FFE195,stroke:#333,stroke-width:4px; + classDef first fill:#9FBCE8 + classDef emphasys fill:#FEB130 +``` + +Now person `5` moves another position ahead by bribing person `3`: + +```mermaid +flowchart LR + A[Ride!]:::first + 1([1]) + 2([2]) + 3([3]):::emphasys + 4([4]) + 5([5]):::emphasys + + A ~~~ 1 + 1 -.- 2 + 2 -.- 5 + 5 -.- 3 + 3 -.- 4 + + classDef default color:#000,fill:#FFE195,stroke:#333,stroke-width:4px; + classDef first fill:#9FBCE8 + classDef emphasys fill:#FEB130 +``` + +And person `2` moves one position ahead by bribing person `3`: + +```mermaid +flowchart LR + A[Ride!]:::first + 1([1]):::emphasys + 2([2]):::emphasys + 3([3]) + 4([4]) + 5([5]) + + A ~~~ 2 + 2 -.- 1 + 1 -.- 5 + 5 -.- 3 + 3 -.- 4 + + classDef default color:#000,fill:#FFE195,stroke:#333,stroke-width:4px; + classDef first fill:#9FBCE8 + classDef emphasys fill:#FEB130 +``` + +So the final state is `2, 1, 5, 3, 4` after three bribing operations. + +### Test Case 2 + +No person can bribe more than two people, yet it appears person `5` has done so. +It is not possible to achieve the input state.