Skip to content

[Hacker Rank] Interview Preparation Kit: Arrays: New Year Chaos. Solv… #180

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
Feb 16, 2025
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,73 @@
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit;

using System.Diagnostics.CodeAnalysis;


/**
* New Year Chaos.
*
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/new_year_chaos.md]]
*/
public class NewYearChaos
{
[ExcludeFromCodeCoverage]
protected NewYearChaos() { }

public const String TOO_CHAOTIC_ERROR = "Too chaotic";

/**
* minimumBribesCalculate.
*/
public static int minimumBribesCalculate(List<int> q)
{
int bribes = 0;
int i = 0;

foreach (int value in q)
{
int position = i + 1;

if (value - position > 2)
{
throw new InvalidOperationException(TOO_CHAOTIC_ERROR);
}

List<int> fragment = q[Math.Min(Math.Max(value - 2, 0), i)..i];

foreach (int k in fragment)
{
if (k > value)
{
bribes += 1;
}
}
i += 1;
}

return bribes;
}

/**
* minimumBribes.
*/
public static String minimumBribesText(List<int> q)
{
try
{
int bribes = minimumBribesCalculate(q);
return String.Format("{0}", bribes);
}
catch (InvalidOperationException e)
{
return String.Format(e.Message);
}
}

/**
* minimumBribesText.
*/
public static void minimumBribes(List<int> q)
{
Console.WriteLine("{0}", minimumBribesText(q));
}
}
Original file line number Diff line number Diff line change
@@ -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
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit;

[TestClass]
public class NewYearChaosTest
{
public class NewYearChaosTestCase
{
public string title { get; set; } = default!;
public List<int> input { get; set; } = default!;
public string expected { get; set; } = default!;
}

private List<NewYearChaosTestCase> testCases { get; set; } = default!;

[TestInitialize]
public void testInitialize()
{
testCases = JsonLoader.resourceLoad<List<NewYearChaosTestCase>>(
"hackerrank/interview_preparation_kit/arrays/new_year_chaos.testcases.json"
) ?? [];
}

[TestMethod]
public void testMinimumBribesText()
{
string result;

foreach (NewYearChaosTestCase test in testCases)
{
result = NewYearChaos.minimumBribesText(test.input);
NewYearChaos.minimumBribes(test.input);

Assert.AreEqual(test.expected, result);
}
}
}

Original file line number Diff line number Diff line change
@@ -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: <https://www.youtube.com/watch?v=LgszjFykAbE>
182 changes: 182 additions & 0 deletions docs/hackerrank/interview_preparation_kit/arrays/new_year_chaos.md
Original file line number Diff line number Diff line change
@@ -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.
Loading