Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank New Year Chaos problem solution

YASH PAL, 31 July 20247 February 2026

In this HackerRank New year chaos interview preparation kit problem solution, you need to determine the minimum number of bribes that took place to get to the given queue order. Print the number of bribes, or, if anyone has bribed more than two people, print Too chaotic.

HackerRank New Year Chaos Interview Preparation kit solution

HackerRank New Year Chaos problem solution in Python.

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the minimumBribes function below.
def minimumBribes(q):
    chaotic = ''
    num_swaps = 0
    for i in range(len(q)):
        if(q[i]>i+3):
            chaotic += 'Too chaotic'
        for j in range(max(0,q[i]-2),i):
            if(q[i]<q[j]):
                num_swaps+=1

    if('Too chaotic' in chaotic):
        print('Too chaotic')
    else:
        print(num_swaps)

if __name__ == '__main__':
    t = int(input())

    for t_itr in range(t):
        n = int(input())

        q = list(map(int, input().rstrip().split()))

        minimumBribes(q)

New Year Chaos problem solution in Java.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner s = new Scanner(System.in);
        int T = s.nextInt();
        int[] nn = new int[100000];
        
        for(int t=0;t<T;t++)
        {
            int n = s.nextInt();
            
            int x, y=0;
            
            for(int j=0;j<n;j++)
            {
                nn[j] = s.nextInt();
                
                if(nn[j]-j > 3)
                {
                    y = -1;
                }
            }
            
            if(y == -1)
            {
                System.out.println("Too chaotic");
                continue;
            }
            
            int yr;
            for(int k=0;k<n;k++)
            {
                yr = y;
                for(int j=0;j<n-1;j++)
                {
                    if(nn[j] > nn[j+1])
                    {
                        x = nn[j];
                        nn[j] = nn[j+1];
                        nn[j+1] = x;
                        y++;
                    }
                }
                if(yr == y)
                    break;
            }
            
            System.out.println(y);
            
        }
    }
}

Problem solution in C++ programming.

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;


int main(){
    int T;
    cin >> T;
    for(int a0 = 0; a0 < T; a0++){
        int n;
        cin >> n;
        vector<int> q(n);
        for(int q_i = 0;q_i < n;q_i++){
           cin >> q[q_i];
        }
        int ans = 0;
        for (int i = n - 1; i >= 0; i--){
            if (ans == -1)
                break;
            int k = i;
            while (q[k] != i + 1)
                k--;
            if (i - k > 2){
                ans = -1;
                break;
            } else {
                while (k != i){
                    swap(q[k], q[k + 1]);
                    k++;
                    ans++;
                }
            }
        }
        if (ans == -1)
            puts("Too chaotic");
        else
            cout << ans << "n";
    }
    return 0;
}

Problem solution in C programming.

#include <stdio.h>
#include <stdlib.h>
int a[100000];

int main(){
  int T,N,c,t,i;
  scanf("%d",&T);
  while(T--){
    scanf("%d",&N);
    for(i=0;i<N;i++)
      scanf("%d",a+i);
    for(i=N-1,c=0;i>=0;i--)
      if(a[i]>i+3){
        printf("Too chaoticn");
        break;
      }
      else if(a[i]>i+1){
        c++;
        t=a[i];
        a[i]=a[i+1];
        a[i+1]=t;
        i+=2;
      }
    if(i<0)
      printf("%dn",c);
  }
  return 0;
}

Problem solution in JavaScript programming.

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
    input_stdin += data;
});

process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("n");
    main();    
});

function readLine() {
    return input_stdin_array[input_currentline++];
}

function main() {
    var T = parseInt(readLine());
    for(var a0 = 0; a0 < T; a0++){
        var n = parseInt(readLine());
        q = readLine().split(' ');
        q = q.map(Number);
        var steps = 0;
        var swaped = true;
        while (swaped) {
            swaped = false;
            for (var i = 0; i < n - 1; i++) {
                var init = q[i];
                if (init - i > 3) {
                    console.log('Too chaotic');
                    swaped = false;
                    break;
                } else if (init > q[i+1]) {
                    swaped = true;
                    steps ++;
                    var temp = init;
                    q[i] = q[i+1];
                    q[i+1] = temp;
                }
            }   
        }
        if (i === n - 1) {
            console.log(steps)            
        }
    }

}

coding problems solutions Hackerrank Problems Solutions interview prepration kit HackerRank

Post navigation

Previous post
Next post

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes