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 Counting Sort 2 problem solution

YASH PAL, 31 July 202423 January 2026

HackerRank Counting Sort 2 problem solution – In this HackerRank Counting Sort 2 problem you have Given an unsorted list of integers, use the counting sort method to sort the list, and then print the sorted list.

Often, when a list is sorted, the elements being sorted are just keys to other values. For example, if you are sorting files by their size, the sizes need to stay connected to their respective files. You cannot just take the size numbers and output them in order, you need to output all the required file information.

The counting sort is used if you just need to sort a list of integers. Rather than using a comparison, you create an integer array whose index range covers the entire range of values in your array to sort. Each time a value occurs in the original array, you increment the counter at that index. At the end, run through your counting array, printing the value of each non-zero valued index that number of times.

Function Description

Complete the countingSort function in the editor below. It should return the original array, sorted ascending, as an array of integers.

countingSort has the following parameter(s):

  • arr: an array of integers
HackerRank Counting Sort 2 problem solution

HackerRank Counting Sort 2 problem solution in Python.

n = int( input() )
ar = [ int(v) for v in input().split() ]

count = [0]*100
for item in ar:
    count[item] += 1

for i in range(100):
    if count[i] > 0:
        print( " ".join( [str(i)]*count[i] ), end=" " )

Counting Sort 2 problem solution in Java.

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

public class Solution {

    public static int[] count(int[] ar, int max){
        int[] counts = new int[max+1];
        for(Integer n : ar){
            counts[n] += 1;
        }
        return counts;
    }

    public static void printCount(int[] counts){
        for(int i = 0; i < counts.length; ++i){
            for(int j = 0; j < counts[i]; ++j){
                System.out.print(i + " ");
            }
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] ar = new int[n];
        for(int i=0;i<n;i++){
            ar[i]=in.nextInt();
        }
        int[] counts = count(ar, 99);
       // printArray(counts);
        printCount(counts);

    }

}

Problem solution in C++.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    int n;
    cin>>n;
    const int LEN = 100;
    int dic[LEN] = {0};
    for(int i=0; i<n; i++){
        int tmp;
        cin >> tmp;
        dic[tmp]++;
    }
    for(int i=0; i<LEN; i++){
        for(int j=0; j<dic[i]; j++){
            cout<< i << " ";
        }
    }
    return 0;
}

Problem solution in C.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int n;
    scanf("%d", &n);
    int ar1 [n];
    for (int i = 0; i < n; i++) {
        scanf("%d", ar1 + i);
    }
    int ar2 [100];
    for (int j = 0; j < 100; j++) {
        ar2[j] = 0;
    }
    count(ar1, ar2, n);
    for(int k = 0; k < 100; k++) {
        int c = ar2[k];
        while(c > 0) {
            printf("%d ", k);
            c--;
        }
    }
    return 0;
}

void count (int* ar1, int *ar2, int size) {
    for(int i = 0; i < size; i++) {
        ar2[(ar1[i])]++;
    }
}

Problem solution in JavaScript.

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

var input = "";

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

function sortNumber(a,b) {
    return a - b;
}


function write(text){
    process.stdout.write(""+text+"n");
}
process.stdin.on('end', function () {
    input = input.split("n");
    
    var array = input[1].split(" ");

    for(var a = 0; a < array.length; a++){
        array[a] = parseInt(array[a]);
    }
    
    var ret = array.sort(sortNumber).join(" ");
    write(ret);
});

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

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