Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
68 views

HackerRank Counting Sort 1 Problem Solution

This document discusses solutions to a HackerRank counting sort problem in multiple programming languages. It provides code samples to count the number of occurrences of each integer value in an input array and return the results in Python, Java, C++, C, and JavaScript. The problem involves reading an integer N and an array of N integers, initializing an output array of size 100 with all zeros, iterating the input array and incrementing the count at the index of each element, and printing the final count array.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

HackerRank Counting Sort 1 Problem Solution

This document discusses solutions to a HackerRank counting sort problem in multiple programming languages. It provides code samples to count the number of occurrences of each integer value in an input array and return the results in Python, Java, C++, C, and JavaScript. The problem involves reading an integer N and an array of N integers, initializing an output array of size 100 with all zeros, iterating the input array and incrementing the count at the index of each element, and printing the final count array.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Home  coding problems  HackerRank Counting Sort 1 problem solution

HackerRank
CLOSE ADS Counting Sort 1 problem solution CLOSE ADS

 YASH PAL  April 20, 2021

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

Search

Subscribe To Channel

Programmingoneonone

YouTube 797

Learn DSA For Free

Problem solution in Python programming.


C d
Code
n = with
Crafted int(input())
 by TemplatesYard | Distributed by Blogger

ar = CLOSE
list(map(int,
ADS input().split())) CLOSE ADS

tot = [0]*100

Most Popular Content


for j in range(0,n):
temp = ar[j] HackerRank Mini-Max Sum
tot[temp] += 1 problem solution
 March 23, 2021
print(*tot, sep =' ')

HackerRank Plus Minus


problem solution

Problem solution in Java Programming.  March 23, 2021

Code HackerRank Time Conversion


import java.io.*; problem solution
 March 23, 2021
import java.util.*;

HackerRank Diagonal
public class Solution { Difference problem solution
 March 23, 2021

public static void main(String[] args) {


Scanner in = new Scanner( System.in ); HackerRank Simple Array Sum
problem solution
in.nextLine(); // not used
 March 23, 2021
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.
Code
#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.


Code
#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
/ Enter your code here. Read input from STDIN. Print output
to STDOUT */
return 0;
}

Problem solution in JavaScript programming.


Code
'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); });

Tags: algorithm coding problems

 Facebook  Twitter    

You might also like