New Year Chaos - Array - Medium - HackerRank
💻 coding

New Year Chaos - Array - Medium - HackerRank

2 min read 421 words
2 min read
ShareWhatsAppPost on X
  • 1The queue allows individuals to bribe the person directly in front of them, with a maximum of two bribes per person.
  • 2The function minimumBribes calculates the minimum number of bribes needed or indicates if the queue state is too chaotic.
  • 3Input consists of multiple test cases, each specifying the final state of the queue represented by an array of integers.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"The queue allows individuals to bribe the person directly in front of them, with a maximum of two bribes per person."

New Year Chaos - Array - Medium - HackerRank

It's New Year's Day and everyone's in line for the Wonderland rollercoaster ride! There are a number of people queued up, and each person wears a sticker indicating their initial position in the queue. Initial positions increment by from at the front of the line to at the back.

Any person in the queue can bribe the person directly in front of them to swap positions. If two people swap positions, they still wear the same sticker denoting their original places in line. One person can bribe at most two others. For example, if and bribes , the queue will look like this: .

Fascinated by this chaotic queue, you decide you must know the minimum number of bribes that took place to get the queue into its current state!

Function Description

Complete the function minimumBribes in the editor below. It must print an integer representing the minimum number of bribes necessary, or Too chaotic if the line configuration is not possible.

minimumBribes has the following parameter(s):

q: an array of integers Input Format

The first line contains an integer , the number of test cases.

Each of the next pairs of lines are as follows: - The first line contains an integer , the number of people in the queue - The second line has space-separated integers describing the final state of the queue.

Constraints

Subtasks

For score For score

Output Format

Print an integer denoting the minimum number of bribes needed to get the queue into its final state. Print Too chaotic if the state is invalid, i.e. it requires a person to have bribed more than people.

Sample Input

2 5 2 1 5 3 4 5 2 5 1 3 4 Sample Output

3 Too chaotic

using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;

class Solution
{

 // Complete the minimumBribes function below.
 static void minimumBribes(int[] q)
 {
 int ans = 0;
 for (int i = q.Length - 1; i >= 0; i--) {
 if (q[i] - (i + 1) > 2) {
 Console.WriteLine("Too chaotic");
 return;
 }
 for (int j = Math.Max(0, q[i] - 2); j < i; j++)
 if (q[j] > q[i]) ans++;
 }
 Console.WriteLine(ans);
 }

 static void Main(string[] args)
 {
 int t = Convert.ToInt32(Console.ReadLine());

 for (int tItr = 0; tItr < t; tItr++)
 {
 int n = Convert.ToInt32(Console.ReadLine());

 int[] q = Array.ConvertAll(Console.ReadLine().Split(' '), qTemp => Convert.ToInt32(qTemp))
 ;
 minimumBribes(q);
 }
 }
}

Time Complexity: O(n)

Space Complexity: O(1)

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

sumitc91

Published on 24 October 2020 · 2 min read · 421 words

Part of AskGif Blog · coding

You might also like