Big Sorting[HackerRank Solution]

1–2 minutes

read

Big Sorting[HackerRank Solution]

Problem:

Consider an array of numeric strings, , where each string is a positive number with anywhere from  to  digits. Sort the array’s elements in non-decreasing (i.e., ascending) order of their real-world integer values and print each element of the sorted array on a new line.

Input Format

The first line contains an integer, , denoting the number of strings in .
Each of the  subsequent lines contains a string of integers describing an element of the array.

Constraints

  • Each string is guaranteed to represent a positive integer without leading zeros.
  • The total number of digits across all strings in  is between  and  (inclusive).

Output Format

Print each element of the sorted array on a new line.

Sample Input 0

6
31415926535897932384626433832795
1
3
10
3
5

Sample Output 0

1
3
3
5
10
31415926535897932384626433832795

Code:

#include <bits/stdc++.h>

using namespace std;

bool check(string str1, string str2)
{
 int n1 = str1.length(), n2 = str2.length();
 
 if (n1 < n2)
 return true;
 if (n2 < n1)
 return false;
 for (int i=0; i<n1; i++)
 {
 if (str1[i] < str2[i])
 return true;
 if (str1[i] > str2[i])
 return false;
 }
 
 return false;
}
int main(){
 int n;
 cin >> n;
 vector<string> s;
 for(int i=0;i<n;i++)
 {
 string s1;
 cin>>s1;
 s.push_back(s1);
 } 
 sort(s.begin(),s.end(),check);
 for(int i=0;i<s.size();i++)
 {
 cout<< s[i] <<endl;
 }
 return 0;
}

Passed all test cases!

5 responses to “Big Sorting[HackerRank Solution]”

  1. Aw, this was a very nice post. In idea I would like to put in writing like this additionally – taking time and precise effort to make a very good article… however what can I say… I procrastinate alot and by no means seem to get one thing done.

    Like

  2. Hello Admin,

    YOU NEED QUALITY VISITORS for your: wordpress.com

    My name is Shawn Jolly, and I’m a Web Traffic Specialist. I can get:
    – visitors from search engines
    – visitors from social media
    – visitors from any country you want
    – very low bounce rate & long visit duration

    CLAIM YOUR 24 HOURS FREE TEST => https://bit.ly/3gqLEoE

    Do not forget to read Review to convince you, is already being tested by many people who have trusted it !!

    Big Sorting[HackerRank Solution] – ADDITIONAL KNOWLEDGE 4 ALL

    Like

  3. I simply want to tell you that I am new to blogs and seriously liked you’re web blog. More than likely I’m planning to bookmark your site . You certainly have tremendous articles and reviews. Bless you for sharing your blog site.

    Like

  4. I just want to tell you that I’m beginner to blogging and absolutely loved this web page. Most likely I’m planning to bookmark your site . You amazingly come with incredible stories. Kudos for sharing your website.

    Like

  5. some really great content on this web site, thanks for contribution.

    Like

Leave a reply to Kristofer Ventrone Cancel reply