Vector Erase Hackerrank Solution in C++ | Hackerrank Solution

Vector Erase Hackerrank Solution 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.

Vector Erase Hackerrank Solution in C++

The following are some useful vector functions:

erase(int position):

Removes the element present at the position.  
Ex: v.erase(v.begin()+4); (erases the fifth 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.)

Input Format

The first line of the input contains an integer N. The next line contains N space-separated integers(1-based index). 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.


1 <= N <= 10^5
1 <= x <= N
1 <= a < b <= N

Output Format

Print the size of the vector in the first line and the elements of the vector after the two erase operations in the second line separated by space.

Sample Input

1 4 6 2 8 9
2 4

Sample Output

1 8 9


The first query is to erase the 2nd element in the vector, which is 4. Then, a modified vector is {1 6 2 8 9}, we want to remove the range of 2~4, which means the 2nd and 3rd elements should be removed. Then 6 and 2 in the modified vector are removed and we finally get {1 8 9}

Submit your solution here: Click here

Vector Erase Hackerrank Solution in C++

#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;

	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;

The Output of Vector Erase Hackerrank Solution

The Output of Vector Erase Hackerrank Solution

Similar to Vector Erase

Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me