|
| 1 | +# [Arrays: New Year Chaos](https://www.hackerrank.com/challenges/new-year-chaos) |
| 2 | + |
| 3 | +Determine how many bribes took place to get a queue into its current state. |
| 4 | + |
| 5 | +- Difficulty: ` #medium ` |
| 6 | +- Category: ` #ProblemSolvingBasic ` |
| 7 | + |
| 8 | +It is New Year's Day and people are in line for the Wonderland rollercoaster ride. |
| 9 | +Each person wears a sticker indicating their initial position in |
| 10 | +the queue from ` 1 ` to ` n `. |
| 11 | + |
| 12 | +Any person can bribe the person directly in front of them to swap positions, |
| 13 | +but they still wear their original sticker. One person can bribe at most two others. |
| 14 | + |
| 15 | +Determine the minimum number of bribes that took place to get |
| 16 | +to a given queue order. |
| 17 | +Print the number of bribes, or, if anyone has bribed more than two people, |
| 18 | +print ` Too chaotic `. |
| 19 | + |
| 20 | +## Example |
| 21 | + |
| 22 | +$ q = [1, 2, 3, 4, 5, 6, 7, 8] $ |
| 23 | + |
| 24 | +If person ` 5 ` bribes person ` 4 `, the queue will look like this: |
| 25 | +$ [1, 2, 3, 5, 4, 6, 7, 8] $. Only ` 1 ` bribe is required. Print ` 1 `. |
| 26 | + |
| 27 | +$ q = [4, 1, 2, 3] $ |
| 28 | + |
| 29 | +Person ` 4 ` had to bribe ` 3 ` people to get to the current position. |
| 30 | +Print `Too chaotic`. |
| 31 | + |
| 32 | +## Function Description |
| 33 | + |
| 34 | +Complete the function minimumBribes in the editor below. |
| 35 | + |
| 36 | +minimumBribes has the following parameter(s): |
| 37 | + |
| 38 | +- `int q[n]`: the positions of the people after all bribes |
| 39 | + |
| 40 | +## Returns |
| 41 | + |
| 42 | +- No value is returned. Print the minimum number of bribes necessary or |
| 43 | + ` Too chaotic ` if someone has bribed more than people. |
| 44 | + |
| 45 | +## Input Format |
| 46 | + |
| 47 | +The first line contains an integer ` t `, the number of test cases. |
| 48 | + |
| 49 | +Each of the next ` t ` pairs of lines are as follows: |
| 50 | + |
| 51 | +- The first line contains an integer ` t `, the number of people in the queue |
| 52 | +- The second line has `n` space-separated integers describing the |
| 53 | +final state of the queue. |
| 54 | + |
| 55 | +## Constraints |
| 56 | + |
| 57 | +- $ 1 \leq t \leq 10 $ |
| 58 | +- $ 1 \leq n \leq 10^5 $ |
| 59 | + |
| 60 | +## Subtasks |
| 61 | + |
| 62 | +For `60%` score $ 1 \leq t \leq 10^3 $ |
| 63 | +For `100%` score $ 1 \leq t \leq 10^5 $ |
| 64 | + |
| 65 | +## Sample Input |
| 66 | + |
| 67 | +```text |
| 68 | +STDIN Function |
| 69 | +----- -------- |
| 70 | +2 t = 2 |
| 71 | +5 n = 5 |
| 72 | +2 1 5 3 4 q = [2, 1, 5, 3, 4] |
| 73 | +5 n = 5 |
| 74 | +2 5 1 3 4 q = [2, 5, 1, 3, 4] |
| 75 | +``` |
| 76 | + |
| 77 | +## Sample Output |
| 78 | + |
| 79 | +```text |
| 80 | +3 |
| 81 | +Too chaotic |
| 82 | +``` |
| 83 | + |
| 84 | +## Explanation |
| 85 | + |
| 86 | +### Test Case 1 |
| 87 | + |
| 88 | +The initial state: |
| 89 | + |
| 90 | +```mermaid |
| 91 | +flowchart LR |
| 92 | +
|
| 93 | + A[Ride!]:::first |
| 94 | + 1([1]) |
| 95 | + 2([2]) |
| 96 | + 3([3]) |
| 97 | + 4([4]) |
| 98 | + 5([5]) |
| 99 | +
|
| 100 | + A ~~~ 1 |
| 101 | + 1 -.- 2 |
| 102 | + 2 -.- 3 |
| 103 | + 3 -.- 4 |
| 104 | + 4 -.- 5 |
| 105 | +
|
| 106 | + classDef default color:#000,fill:#FFE195,stroke:#333,stroke-width:4px; |
| 107 | + classDef first fill:#9FBCE8 |
| 108 | + classDef emphasys fill:#FEB130 |
| 109 | +``` |
| 110 | + |
| 111 | +After person `5` moves one position ahead by bribing person `4`: |
| 112 | + |
| 113 | +```mermaid |
| 114 | +flowchart LR |
| 115 | + A[Ride!]:::first |
| 116 | + 1([1]) |
| 117 | + 2([2]) |
| 118 | + 3([3]) |
| 119 | + 4([4]):::emphasys |
| 120 | + 5([5]):::emphasys |
| 121 | +
|
| 122 | + A ~~~ 1 |
| 123 | + 1 -.- 2 |
| 124 | + 2 -.- 3 |
| 125 | + 3 -.- 5 |
| 126 | + 5 -.- 4 |
| 127 | +
|
| 128 | + classDef default color:#000,fill:#FFE195,stroke:#333,stroke-width:4px; |
| 129 | + classDef first fill:#9FBCE8 |
| 130 | + classDef emphasys fill:#FEB130 |
| 131 | +``` |
| 132 | + |
| 133 | +Now person `5` moves another position ahead by bribing person `3`: |
| 134 | + |
| 135 | +```mermaid |
| 136 | +flowchart LR |
| 137 | + A[Ride!]:::first |
| 138 | + 1([1]) |
| 139 | + 2([2]) |
| 140 | + 3([3]):::emphasys |
| 141 | + 4([4]) |
| 142 | + 5([5]):::emphasys |
| 143 | +
|
| 144 | + A ~~~ 1 |
| 145 | + 1 -.- 2 |
| 146 | + 2 -.- 5 |
| 147 | + 5 -.- 3 |
| 148 | + 3 -.- 4 |
| 149 | +
|
| 150 | + classDef default color:#000,fill:#FFE195,stroke:#333,stroke-width:4px; |
| 151 | + classDef first fill:#9FBCE8 |
| 152 | + classDef emphasys fill:#FEB130 |
| 153 | +``` |
| 154 | + |
| 155 | +And person `2` moves one position ahead by bribing person `3`: |
| 156 | + |
| 157 | +```mermaid |
| 158 | +flowchart LR |
| 159 | + A[Ride!]:::first |
| 160 | + 1([1]):::emphasys |
| 161 | + 2([2]):::emphasys |
| 162 | + 3([3]) |
| 163 | + 4([4]) |
| 164 | + 5([5]) |
| 165 | +
|
| 166 | + A ~~~ 2 |
| 167 | + 2 -.- 1 |
| 168 | + 1 -.- 5 |
| 169 | + 5 -.- 3 |
| 170 | + 3 -.- 4 |
| 171 | +
|
| 172 | + classDef default color:#000,fill:#FFE195,stroke:#333,stroke-width:4px; |
| 173 | + classDef first fill:#9FBCE8 |
| 174 | + classDef emphasys fill:#FEB130 |
| 175 | +``` |
| 176 | + |
| 177 | +So the final state is `2, 1, 5, 3, 4` after three bribing operations. |
| 178 | + |
| 179 | +### Test Case 2 |
| 180 | + |
| 181 | +No person can bribe more than two people, yet it appears person `5` has done so. |
| 182 | +It is not possible to achieve the input state. |
0 commit comments