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

Saturday 20 May 2023

Sample Task and Solution : Sets-STL in C++ of HackerRank

Sets are a part of the C++ STL. Sets are containers that store unique elements following a specific order. Here are some of the frequently used member functions of sets:

  • Declaration:

    set<int>s; //Creates a set of integers.
    
  • Size:

    int length=s.size(); //Gives the size of the set.
    
  • Insert:

    s.insert(x); //Inserts an integer x into the set s.
    
  • Erasing an element:

    s.erase(val); //Erases an integer val from the set s.
    
  • Finding an element:

    set<int>::iterator itr=s.find(val); //Gives the iterator to the element val if it is found otherwise returns s.end() .
    Ex: set<int>::iterator itr=s.find(100); //If 100 is not present then it==s.end().
    

    To know more about sets click Here. Coming to the problem, you will be given  queries. Each query is of one of the following three types:

      : Add an element  to the set.
      : Delete an element  from the set. (If the number  is not present in the set, then do nothing).
      : If the number  is present in the set, then print "Yes"(without quotes) else print "No"(without quotes).

Input Format

The first line of the input contains  where  is the number of queries. The next  lines contain  query each. Each query consists of two integers  and  where  is the type of the query and  is an integer.

Constraints



Output Format

For queries of type  print "Yes"(without quotes) if the number  is present in the set and if the number is not present, then print "No"(without quotes).
Each query of type  should be printed in a new line.

Sample Input

8
1 9
1 6
1 10
1 4
3 6
3 14
2 6
3 6

Sample Output

Yes
No
No

Solution

#include<bits/stdc++.h>
using namespace std;
int main() {
int q,x,y;
cin>>q;

set<int> s;
set<int> :: iterator iter;

while(q>0){
    cin>>y>>x;
    q--;

    switch (y) {
        case 1:
        s.insert(x);  
        break;

        case 2:
        s.erase(x);
        break;

        case 3:
        iter = s.find(x);
        if(iter!=s.end()){
            cout<<"Yes"<<endl;
        }
        else {
            cout<<"No"<<endl;
        }
    }
}

return 0;
}



 Source : HackerRank

Sample Task and Solution : Lower Bound in C++ of HackerRank

 You are given  integers in sorted order. Also, you are given  queries. In each query, you will be given an integer and you have to tell whether that integer is present in the array. If so, you have to tell at which index it is present and if it is not present, you have to tell the index at which the smallest integer that is just greater than the given number is present.

Lower bound is a function that can be used with a sorted vector. Learn how to use lower bound to solve this problem by clicking here.

Input Format

The first line of the input contains the number of integers . The next line contains  integers in sorted order. The next line contains , the number of queries. Then  lines follow each containing a single integer .

Note: If the same number is present multiple times, you have to print the first index at which it occurs. Also, the input is such that you always have an answer for each query.

Constraints


  • ,where  is  element in the array.


Output Format

For each query you have to print "Yes" (without the quotes) if the number is present and at which index(1-based) it is present separated by a space.

If the number is not present you have to print "No" (without the quotes) followed by the index of the next smallest number just greater than that number.

You have to output each query in a new line.

Sample Input

 8
 1 1 2 2 6 9 9 15
 4
 1
 4
 9
 15

Sample Output

 Yes 1
 No 5
 Yes 6
 Yes 8

Solution

#include<bits/stdc++.h>
using namespace std;
int main() {
long n, val,q;
cin>>n;
vector<long>v(n);
for(int i=0; i<v.size(); i++){
    cin>>v[i];
}
cin>>q;
for(int i=0; i<q; i++){
    cin>>val;
    vector<long>::iterator p =lower_bound(v.begin(), v.end(), val);
    if (val!=*p){
    cout<<"No" <<" "<< (p-v.begin()+1)<<endl;
    }
    else {
    cout<<"Yes" <<" "<< (p-v.begin()+1)<<endl;
    }
}
return 0;
}


Source : HackerRank