- In the solution of Vector-sort you will get the knowledge about how to sort the vectors in c++ and how to use the functions of vectors in c++.
- Before moving further we recommend you to read this vector in c++
The points are mention below are discussed in this article
- problem analysis
- input format
- output format
- sample input
- sample output
- coding
- coding explanation
- feedback
Vector-sort in C++ |
Problem Analysis
- In this vector-sort Hacker Rank problem given us to take an input "n" number of integers from the user and print out the vectors in a sorted order.
- for example if we enter the size of vector is 4 then program will ask us to input 4 integers then if we entered like (9,2,6,1) after sort out the vector it will show us like (1,2,6,9) .
- in this problem we are going use the vector functions are like ( begin() , end() , push_back() ) .
Input format
The first line of the input contains where is the number of integers. The next line contains integers.
Output format
Print the integers in the sorted order one by one in a single line followed by a space.
Sample input
4
9 2 6 1
Sample output
1 2 6 9
Coding
#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>vec;
int n, nums;
cin >> n;
while (cin >> nums)
vec.push_back(nums);
sort(vec.begin(), vec.end());
for(int i=0; i<n; i++)
cout << vec[i] << " ";
return 0;
}
Coding explanation
- First of all declared the required header files for vector and for sorting ,here to use the sort keyword we use the header file (#include<algorithm>) .
- Then declare the vector of type int of name vec, then we declare two variables for taking size of vector and number of vectors.
- then we declare a while loop which take inputs of the vectors and push_back into vector and then by using sort keyword we sort the array and display it through the for loop.
Also check Out
Q-2) pointers in c
Q-3) Vector Sort
Also check Out Projects for Resume
Projects on C++
Projects on C
Problems On C
Q-1) Simple Calculator
Problems On C++
Q-2) Vectors In c++
Feedback
- That is for Vector-sort in C++ Hacker Rank solution hope you enjoy the article and understand this explanation clearly , if still have you any doubts please comment below and if any suggestion for us give us through the comment and please share the article with your friends if you like this.
THANKYOU!!
Post a Comment
Post a Comment
Please do not spam in comment