HackerRank C++ Vector-Erase




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.



  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7. int main() {
  8. int n,i,b,c,d,size;
  9. cin>>n;
  10. vector<int> a;
  11. for(i=0;i<n;i++)
  12. {
  13. int x;
  14. cin>>x;
  15. a.push_back(x);
  16. }
  17. cin>>b>>c>>d;
  18. a.erase(a.begin()+b-1);
  19. a.erase(a.begin()+c-1,a.begin()+d-1);
  20. size=a.size();
  21. cout<<size<<endl;
  22. for(i=0;i<size;i++)
  23. cout<<a[i]<<" ";
  24. return 0;
  25. }
Please click on the like button if it worked

Solution not working or have any suggestions? Please send an email to [email protected]


donate a cup of tea :)


Join Our Facebook Group

Share this solution






codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube