Friday, January 1, 2021

Vector-Erase | HackerRank | C++ | Solution



Problem : https://www.hackerrank.com/challenges/vector-erase/problem

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 ;
    cin>>n;
    vector<int>  v;
    int temp;
    for(int i =0;i<n;i++){
        cin>>temp;
        v.push_back(temp);
    }
    // 1 4 6 2 8 9
    
    int pos1; // 2
    cin>>pos1;
    
    v.erase(v.begin()+pos1-1);
    
    // 1 6 2 8 9
    
    int a, b;  // a = 2 and b = 4
    cin>>a>>b;
    
    
    v.erase(v.begin()+a-1,v.begin()+b-1);
    
    
    // 1 8 9
    
    
    cout<<v.size()<<endl;
    for(int i:v){
        cout<<i<<" ";
    }
    
    
}

No comments:

Post a Comment