Tuesday, 13 September 2016

Hacker Rank Solution Program In C++ For " Vector-Sort"

Problem :- Hacker Rank Solution Program In C++ For " Vector-Sort"

Declaration:
vector<int>v; (creates an empty vector of integers)

Size:

int size=v.size();

Pushing an integer into a vector:

v.push_back(x);(where x is an integer.The size increases by 1 after this.)

Popping the last element from the vector:

v.pop_back(); (After this the size decreases by 1)

Sorting a vector:

sort(v.begin(),v.end()); (Will sort all the elements in the vector)

Solution:-

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

int main() 
{
    int n,i;
    cin>>n;
    vector<int> arr(n);
    for(i=0;i<n;i++)
        cin>>arr[i];
    sort(arr.begin(),arr.end());
    for(i=0;i<n;i++)
        cout<<arr[i]<<" ";
    
    return 0;
}

Output :-
Hacker Rank Solution Program In C++ For " Vector-Sort"



Hacker Rank Solution Program In C++ For " Vector-Sort"


No comments:
Write comments

Recommended Posts × +