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
      • Data Structures Solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Counting Sort 1 problem solution

YASH PAL, 31 July 202423 January 2026

HackerRank Counting Sort 1 problem solution – In this HackerRank Counting Sort 1 problem, you are given a list of integers, count and return the number of times each value appears as an array of integers.

Comparison Sorting
Quicksort usually has a running time of n x log(n), but is there an algorithm that can sort even faster? In general, this is not possible. Most sorting algorithms are comparison sorts, i.e. they sort a list just by comparing the elements to one another. A comparison sort algorithm cannot beat n x log(n) (worst-case) running time, since n x log(n) represents the minimum number of comparisons needed to know where to place each element.

Alternative Sorting
Another sorting method, the counting sort, does not require comparison. Instead, 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.

countingSort has the following parameter(s):

  • arr[n]: an array of integers

Returns

  • int[100]: a frequency array
HackerRank Counting Sort 1 problem solution

HackerRank Counting Sort 1 problem solution in Python.

n = int(input())
ar = list(map(int, input().split()))

tot = [0]*100

for j in range(0,n):
    temp = ar[j]
    tot[temp] += 1
print(*tot, sep =' ')    

Counting Sort 1 problem solution in Java.

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

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner( System.in );
        in.nextLine();  // not used
        System.out.println( outputString( count( convertToInts( in.nextLine().split( " " ) ) ) ) );
    }
    
    public static int[] count( int[] ar ) {
        int[] count = new int[100];
        for( int nbr : ar ) {
            count[nbr] += 1;
        }
        return count;
    }
    
    private static String outputString( int[] ar ) {
        StringJoiner joiner = new StringJoiner( " " );
        for( Integer value : ar ) {
            joiner.add( value.toString() );
        }
        return joiner.toString();
    }
    
    private static int[] convertToInts( String[] values ) {
        int[] parsed = new int[values.length];
        for( int i = 0; i < values.length; i++ ) {
            parsed[i] = Integer.valueOf( values[i] );
        }
        return parsed;
    }
}

Problem solution in C++ programming.

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

int main() {
	int n, val;
	int A[100];
	for (int i = 0; i < 100; i++) {
		A[i] = 0;
	}
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &val);
		A[val]++;
	}
	printf("%d", A[0]);
	for (int i = 1; i < 100; i++) {printf(" %d", A[i]);}
	printf("n");
	return 0;
}

Problem solution in C programming.

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

int main() 
{
    int i,j,n,a[1000001];
    int c[100]={0};
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        c[a[i]]++;
    }
    for(i=0;i<100;i++)
        printf("%d ",c[i]);
    printf("n");
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}

Problem solution in JavaScript programming.

'use strict';

function processData(input) {
    var lines = input.split('n'),
        len = parseInt(lines[0], 10);

    lines = lines[1].split(" ");

    var A = Array.apply(null, new Array(100)).map(Number.prototype.valueOf, 0);

    for (var i=0; i<len; i++) {
        A[parseInt(lines[i], 10)]++;
    }

    console.log(A.join(" "));
}

process.stdin.resume();
process.stdin.setEncoding("ascii");
var _input = "";
process.stdin.on("data", function (input) { _input += input; });
process.stdin.on("end", function () { processData(_input); });

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