Recent Post

Vector-Erase.cpp-HackerRank

Problem-You are provided with a vector of  integers. Then, you are given  queries. For the first query, you are provided with  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  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.

Solution-

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    long long int n,a,b,z,x,i;
    cin>>n;
    vector <long long int> v(n,0);
    for(i=0;i<n;i++)
        cin>>v[i];
    cin>>x;
    cin>>a>>b;
    x=x-1;
    a=a-1;
    b=b-1;
    v.erase(v.begin()+x);
    v.erase(v.begin()+a,v.begin()+b);
    cout<<v.size()<<endl;
    for(i=0;i<v.size();i++)
        cout<<v[i]<<" ";
}

Link to the problem-

Result!

Comments

Popular posts from this blog

Caesar Cipher.c-HackerRank

Bon Appétit.c-HackerRank

Electronics Shop.c-HackerRank