In this HackerRank Vector-Erase problem in c++ you are provided with a vector of N integers. Then, you are given 2 queries. For the first query, you are provided with 1 integer, which denotes a position in the vector. The value at this position in the vector needs to be erased. The next query consists of 2 integers denoting a range of the positions in the vector. The elements which fall under that range should be removed. The second query is performed on the updated vector which we get after performing the first query.

HackerRank Vector-Erase in C++ problem solution


HackerRank Vector-Erase in C++ problem solution

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

vector<int> v;
int i,n,x,m,a,b;
int main() {
    cin>>n;
   
    for(i=0;i<n;i++)
    { cin>>x;
      v.push_back(x);
    }
    cin>>x;
    v.erase(v.begin()+x-1);
    
    cin>>a>>b;
    v.erase(v.begin()+a-1,v.begin()+b-1);
    
    m=v.size();
    cout<<m<<endl;
    cout <<v.at(0);
    for ( i=1; i<m; i++)
       cout <<' '<<v.at(i);
    cout<<endl;
    
    return 0;
}