New Year Chaos Solution - The Coding Shala
New Year Chaos Problem Solution
New Year Chaos Solution in Java
Approach / Explanation
First, eliminate the chaotic case. Any person who is more than 2 positions ahead[left side] from their original position can not be there so return "Too chaotic" for that.
we know only a maximum of two bribes allowed so a person can only move back by number-2. For num, inner loop will be [num-2, arr.length-1] or [0, arr.length-1] (if num-2<0). The bribe count is all the numbers that are greater than the current number.
Let's understand this by an example:
Here, person 4 is not its original position so all the numbers that are greater than 4 must have bribed 4 at some time and move to the left. 4 have [6,8,7, and 5] numbers ahead that mean these people must have bribed 4 and moved to left.
Note: Here 4 has not bribed anyone because it is not ahead of its original position.
Like this 7, and 8 are ahead of 6 and we can find for others in the same way.
In the above example the total number of bribes are:
So we need to check through the queue in front of you and count all the numbers that are greater than you and that are the total number of bribes.
Other Posts You May Like
- LeetCode - Climbing Stairs
- LeetCode - Jewels and Stones
- Fibonacci Series
- LeetCode - Contains Duplicate
- Maximum Absolute Difference