Tuesday, 13 September 2016

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

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

erase(int position):

Removes the element present at position.

Ex: v.erase(v.begin()+4); (erases the 5th element of the vector v)

erase(int start,int end):

Removes the elements in the range from start to end inclusive of the start and exclusive of the end.

Ex:v.erase(v.begin()+2,v.begin()+5);(erases all the elements from the third element to the fifth element.)

Solution :-

#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 */ 
    int n,i,b,c,d,size;
    cin>>n;
    vector<int> a;
    for(i=0;i<n;i++)
        {
        int x;
        cin>>x;
        a.push_back(x);
        }
    cin>>b>>c>>d;
    a.erase(a.begin()+b-1);
    a.erase(a.begin()+c-1,a.begin()+d-1);
    
    size=a.size();
    
    cout<<size<<endl;
    for(i=0;i<size;i++)
        cout<<a[i]<<" ";
    return 0;
}

Output :-


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

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


No comments:
Write comments

Recommended Posts × +