Skip to content
Programmingoneonone
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

  • Home
  • CS Subjects
    • IoT ? Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

HackerRank Big Sorting problem solution

YASH PAL, 31 July 2024

In this HackerRank Big Sorting problem, Consider an array of numeric strings where each string is a positive number with anywhere from 1 to (10)power 6 digits. Sort the array’s elements in non-decreasing, or ascending order of their integer values and return the sorted array.

HackerRank Big Sorting problem solution

Topics we are covering

Toggle
  • Problem solution in Python programming.
  • Problem solution in Java Programming.
    • Problem solution in C++ programming.
    • Problem solution in C programming.
    • Problem solution in JavaScript programming.

Problem solution in Python programming.

#!/bin/python3

import sys


n = int(input().strip())
unsorted = []
unsorted_i = 0
for unsorted_i in range(n):
    unsorted_t = str(input().strip())
    unsorted.append(unsorted_t)
for v in sorted(unsorted, key = lambda v: int(v)):
    print(v)

Problem solution in Java Programming.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        String[] unsorted = new String[n];
        for(int unsorted_i=0; unsorted_i < n; unsorted_i++){
            unsorted[unsorted_i] = in.next();
        }
        Map<Integer, ArrayList<String>> map = new TreeMap<>();
        for(int i = 0; i < n; i++) {
            ArrayList<String> mapValue = map.get(unsorted[i].length());
            if(mapValue == null) {
                mapValue = new ArrayList<>();
                mapValue.add(unsorted[i]);
                map.put(unsorted[i].length(), mapValue);
            } else {
                mapValue.add(unsorted[i]);
            }
        }
        
        for(ArrayList<String> list : map.values()) {
            Collections.sort(list);
        }
        
        for(Integer i : map.keySet()) {
            System.out.println(Arrays.toString(map.get(i).toArray(new String[0])).replaceAll("[\[\],]", "").replaceAll(" ", "n"));
        }
    }
}

Problem solution in C++ programming.

#include <bits/stdc++.h>

using namespace std;

int main(){
    int n;
    cin >> n;
    vector<string> unsorted(n);
    for(int unsorted_i = 0; unsorted_i < n; unsorted_i++){
       cin >> unsorted[unsorted_i];
    }

    sort(unsorted.begin(), unsorted.end(), [](const string& a, const string& b) {
        if (a.length() != b.length()) {
            return a.length() < b.length();
        }
        return a < b;
    });
    
    for (auto x:unsorted) cout << x << endl;
    
    return 0;
}

Problem solution in C programming.

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

static int compare (void const *a, void const *b)
{
    char const *const *pa = a;
    char const *const *pb = b;
    if(strlen(*pa) == strlen(*pb))
        return strcmp(*pa, *pb);
    return strlen(*pa) - strlen(*pb);
}


int main(){
    int nbr;
    scanf("%d", &nbr);
    char **tab = malloc(sizeof(char*) * nbr);
    char *str = (char*)malloc(sizeof(char) * 1000000);
    for(int i = 0; i < nbr; i++)
    {
        scanf("%s", str);
        tab[i] = malloc(sizeof(char) * (strlen(str) + 1));
        sprintf(tab[i],"%s",str);
    }
    qsort(tab, nbr, sizeof(char*), compare);
    for(int i = 0; i < nbr; i++)
        printf("%sn", tab[i]);
    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++];
}

var BigNumber = require('bignumber.js');

/////////////// ignore above this line ////////////////////

function main() {
    var n = parseInt(readLine());
    var unsorted = [];
    for(var unsorted_i = 0; unsorted_i < n; unsorted_i++){
       var big = new BigNumber(readLine());
       unsorted[unsorted_i] = big;
    }
    
    unsorted = unsorted.sort(function (a,b){
       return a.minus(b); 
    });

    for(let i = 0; i < unsorted.length; i++){
        console.log(unsorted[i].toFixed());
    }
}

Algorithms coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes