<\/span><\/h2>\n\n\n\nYou are given N integers.Sort the N integers and print the sorted order.<\/p>\n\n\n\n
Store the N integers in a vector.Vectors are sequence containers representing arrays that can change in size.<\/p>\n\n\n\n
Declaration:<\/p>\n\n\n\n
vector<int>v; (creates an empty vector of integers)\n<\/code><\/pre>\n\n\n\nSize:<\/p>\n\n\n\n
\n int size=v.size();\n\n Pushing an integer into a vector:\n\n v.push_back(x);(where x is an integer.The size increases by 1 after this.)\n\n Popping the last element from the vector:\n\n v.pop_back(); (After this the size decreases by 1)\n<\/code><\/pre>\n\n\n\nSorting a vector:<\/p>\n\n\n\n
sort(v.begin(),v.end()); (Will sort all the elements in the vector)\n<\/code><\/pre>\n\n\n\n<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nThe first line of the input contains N where N is the number of integers. The next line contains N integers.<\/p>\n\n\n\n
<\/span>Constraints<\/strong><\/span><\/h2>\n\n\n\n1 <= n <= 105<\/sup><\/p>\n\n\n\n1 <= Vi<\/sub> <= 109<\/sup> , where Vi<\/sub> is the ith <\/sup>integer in the vector.<\/p>\n\n\n\n<\/span>Output Format<\/strong><\/span><\/h2>\n\n\n\nPrint the integers in the sorted order one by one in a single line followed by a space.<\/p>\n\n\n\n
Sample Input<\/strong><\/p>\n\n\n\n5\n1 6 10 8 4\n<\/code><\/pre>\n\n\n\nSample Output<\/strong><\/p>\n\n\n\n1 4 6 8 10\n<\/code><\/pre>\n\n\n\n<\/span>Solution – Vector Sort HackerRank Solution in C++<\/strong><\/span><\/h2>\n\n\n\n#include <cmath>\n#include <cstdio>\n#include <vector>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\n\n\nint main() {\n \/* Enter your code here. Read input from STDIN. Print output to STDOUT *\/ \n int n; cin >> n;\n vector<int> arr;\n int x;\n for(int i=0 ; i<n ;i++)\n {\n cin >> x;\n arr.push_back(x);\n }\n sort(arr.begin(),arr.end());\n for(int i=0 ; i<arr.size();i++)\n {\n cout<< arr[i] <<' ';\n }\n return 0;\n}\n<\/pre>\n\n\n\n