Header Ad

HackerRank Vector-Sort solution in c++ programming

In this HackerRank vector-sort problem in the c++ programming language, You are given N integers.Sort the N integers and print the sorted order.

Store the N integers in a vector.Vectors are sequence containers representing arrays that can change in size.


HackerRank Vector-Sort solution in c++ programming


HackerRank Vector-Sort problem solution in c++ programming.

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


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
    vector<int> v;
int size;
cin>>size;
int a;
for(int i=0;i<size;i++)
{
 cin>>a;
 v.push_back(a);
}
sort(v.begin(),v.end());
 for(int i=0;i<size;i++)
    {
      cout<<v[i]<<" ";
    }
return 0;  
    return 0;
}


Second solution

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


int main() {
    int N = 0;
    cin>>N;
    
    vector<int> vecInt;
    int number = 0;
    
    while (cin>>number)
        vecInt.push_back(number);
    
    sort(vecInt.begin(), vecInt.end());
    
    for ( vector<int>::iterator it = vecInt.begin(); it != vecInt.end(); it++)
        cout<<*it<<" ";
    return 0;
}

Post a Comment

0 Comments