Vector-Erase in C++ – Hacker Rank Solution | HackerRank Programming Solutions | HackerRank C++ Solutions

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank of Programming Language C++ . At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight you profile to the recruiters.

In this post, you will find the solution for Vector-Erase in C++-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

Introduction To C++

C++ is a general-purpose programming language that was developed as an enhancement of the C language to include object-oriented paradigm. It is an imperative and a compiled language. 

C++ is a middle-level language rendering it the advantage of programming low-level (drivers, kernels) and even higher-level applications (games, GUI, desktop apps etc.). The basic syntax and code structure of both C and C++ are the same. 

C++ programming language was developed in 1980 by Bjarne Stroustrup at bell laboratories of AT&T (American Telephone & Telegraph), located in U.S.A. Bjarne Stroustrup is known as the founder of C++ language.

Vector-Erase in C++ – Hacker Rank Solution 


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.
The following are some useful vector functions:

erase(int position) :

Removes the element present at 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.

Constraints :

  • 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

Explanation :

The first query is to erase the 2nd element in the vector, which is 4. Then, modifed 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}

Vector-Erase in C++ – Hacker Rank 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 */ 
    /* Vector-Erase in C++ - Hacker Rank Solution START */
    vector<int> v ;
    int x,i,j; 
    int n; 
    for(i =0 ; i<n ; i++)
    int q1, 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 << << " " ;  
    /* Vector-Erase in C++ - Hacker Rank Solution END */
return 0;

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock