Sunday, 6 November 2016

Hacker Rank Solution Program In C++ For " Sets-STL "

In C++ Sets ( are containers that store unique elements ) are a Important part of the STL ( Standard Template Library ) .Mostly used member functions of sets are:

Declaration:

set<int>st; //Creates a set of integers.
//st is a name of set

Size:

int length=s.size(); //Gives the size of the set.
//length is a size of 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.

Go To Question On Hacker Rank Click Here

Solution :-

#include <cmath>

#include <cstdio>
#include <vector>
#include <iostream>
#include <set>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int n;
    set<int>s;
    for(int i=0;i<n;i++)
        {
        int a;
        cin>>a;
        s.insert(a);
        }
    cin>>n;
    for(int i=0;i<n;i++)
        {
        int q,t;
        cin>>t>>q;
        switch(t)
            {
            case 1:
            {
                s.insert(q);
                break;
            }
            case 2:
            {
                s.erase(q);
                break;
            }
            case 3:
            {
                set<int>::iterator itr=s.find(q);
                if(itr==s.end())
                   cout<<"No"<<endl;
                else
                   cout<<"Yes"<<endl;
                break;
            }
            }
        }
 
    return 0;
}

Output :-

1.Compilation With Custom Input

Hacker Rank Solution Program In C++ For " Sets-STL "

2.All Testcase's 

Hacker Rank Solution Program In C++ For " Sets-STL "


No comments:
Write comments

Recommended Posts × +