I was trying to solve a very simple coding question:
Consider an array of numeric strings where each string is a positive number with anywhere from 1 to 10^6 digits. Sort the array's elements in non-decreasing, or ascending order of their integer values and print each element of the sorted array on a new line.
The first line contains an integer n denoting the number of strings Each of the n subsequent lines contain an INTEGER STRING.
My code is:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int i; string s;
cin >> i;
int j = i;
string arr[i];
int cntr = 0;
while (i--){
cin >> s;
arr[cntr] = s;
cntr++;
}
sort(arr, arr+j);
for (auto c: arr)
cout << c << endl;
}
The input is
6
31415926535897932384626433832795
1
3
10
3
5
And my output turns out to be:
1
10
3
3
31415926535897932384626433832795
5
If I make an array and add integer strings to it manually, the above code works fine. Then why is it producing wrong result when it takes input from the website?
PS: Here's the link to the problem:https://www.hackerrank.com/challenges/big-sorting/problem