- In the solution of Vector-Erase you will get the knowledge about how to erase elements of vectors in c++ and how to use the functions of vectors -Erase in c++.
- Before moving we recommended you to read this article vectors in c++
The points are mention below are discussed in this article
- problem analysis
- input format
- output format
- sample input
- sample output
- coding
- coding explanation
- explain in vs code
- feedback
Problem Analysis
Input format
- In the first line of the input contains an integer 'n' the next line contains 'N' space separated by integers .
- The third line contains a single integer x denoting the position of an element that should be removed from the vector .
- The fourth line contains two integers a and b denoting the range that should be erased from the vector inclusive of a and exclusive of b.
Output format
- Print the size of the vector in the first line and print the elements of the vector after the two erase operations in the second line separated by a space.
Sample input
Sample output
Coding
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v ;
int x,i,j;
int n;
cin>>n;
for(i =0 ; i<n ; i++)
{
cin>>x;
v.push_back(x);
}
int q1, q2, q3;
cin>>q1;
cin>>q2>>q3;
v.erase (v.begin()+(q1-1));
v.erase(v.begin()+q2-1 , v.begin()+q3-1);
cout << v.size() << endl;
for(j =0 ; j< v.size() ; j++)
{
cout << v.at(j) << " " ;
}
return 0;
}
Coding explanation
- First of all declared the required header files for the vector-erase solution.
- Then we declare the vector of int type of name 'v' after that we declare some variables for taking the size of vectors and for positions to erase elements.
- Here 'n' variable is for taking the size of the vector and 'x' variable is for taking input the vectors by the help of push_back function.
- Then we declare three variable of name q1,q2,q3 for erasing the queries ,in first line we take the 1st query to erase from the vector and then we take two more positions for erasing the elements.
- Now we use the erase function for deleting values for the 1st query and second query then we print the size of vector now present in vector and then we print in a next line the elements are present in vector.
Vs code example for reference not to use in hacker rank
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v ;
int x,i,j;
int n,count=1;
cout<<"Enter the size of vector:"<<endl;
cin>>n;
for(i =0 ; i<n ; i++)
{
cout<<"Enter the elements no. :"<<count++<<endl;
cin>>x;
v.push_back(x);
}
int q1, q2, q3;
cout<<"Enter the first position that you want to erase:"<<endl;
cin>>q1;
cout<<"Enter the next two positions for erasing the data:"<<endl;
cin>>q2>>q3;
v.erase (v.begin()+(q1-1));
v.erase(v.begin()+q2-1 , v.begin()+q3-1);
cout<<"The number element present now in vector is:"<<endl;
cout << v.size() << endl;
cout<<"The vectors are:"<<endl;
for(j =0 ; j< v.size() ; j++)
{
cout << v.at(j) << " " ;
}
return 0;
}
Output
Also check Out
Also check Out Projects for Resume
Feedback
- That is for Vector-erase in C++ Hacker Rank solution hope you enjoy the article and understand this explanation clearly , if still have you any doubts please comment below and if any suggestion for us give us through the comment and please share the article with your friends if you like this.
Post a Comment
Post a Comment
Please do not spam in comment