Showing posts with label HackerRank. Show all posts
Showing posts with label HackerRank. Show all posts

Friday 19 May 2023

Sample Task and Solution : Vector Erase in C++ of HackerRank

You are provided with a vector of  integers. Then, you are given  queries. For the first query, you are provided with  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  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 .The next line contains  space separated integers(1-based index).The third line contains a single integer ,denoting the position of an element that should be removed from the vector.The fourth line contains two integers  and  denoting the range that should be erased from the vector inclusive of a and exclusive of b.

Constraints


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

6
1 4 6 2 8 9
2
2 4

Sample Output

3
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}

My 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;
    vector<int>v;
   
    cin>>n;
    for(auto i=0; i< n;i++){
        int v2;

        cin>>v2;
        v.push_back(v2);
    }
   


    int er, er2,er3;
    cin>>er;
    v.erase(v.begin()+(er-1));
    cin>>er2;
    cin>>er3;
    v.erase(v.begin()+(er2-1),v.begin()+(er3-1));

    cout << v.size()<<endl;
    for (auto x : v)
        cout << x << " ";

   
}



 Source : HackerRank

Thursday 18 May 2023

Sample Task and Solution : Vector-sort in C++ of HackerRank

 You are given  integers.Sort the  integers and print the sorted order.

Store the  integers in a vector.Vectors are sequence containers representing arrays that can change in size.

  • Declaration:

    vector<int>v; (creates an empty vector of integers)
    
  • Size:

    int size=v.size();
    
  • Pushing an integer into a vector:

    v.push_back(x);(where x is an integer.The size increases by 1 after this.)
    
  • Popping the last element from the vector:

    v.pop_back(); (After this the size decreases by 1)
    
  • Sorting a vector:

    sort(v.begin(),v.end()); (Will sort all the elements in the vector)
    

To know more about vectors, Click Here

Input Format

The first line of the input contains  where  is the number of integers. The next line contains  integers.
Constraints

, where  is the  integer in the vector.

Output Format

Print the integers in the sorted order one by one in a single line followed by a space.

Sample Input

5
1 6 10 8 4

Sample Output

1 4 6 8 10

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;
    vector<int>v;
   
    cin>>n;
    for(auto i=0; i< n;i++){
        int v2;
       
        cin>>v2;
        v.push_back(v2);
    }
   
    sort(v.begin(), v.end());

    for (auto x : v)
        cout << x << " ";
    return 0;
}


Source : HackerRank