C++ Sets | Practice | GeeksforGeeks

Given two sets A and B of size N and M . Perform set operation i.e union, intersection and difference on sets using set_union, set_difference and set_intersection functions of STL.

Example 1:
Input: N = 4, M = 5
A = {1, 2, 3, 4}
B = {3, 4, 5, 6, 7}
Output:
1 2 3 4 5 6 7
1 2
3 4
Explaination: 
Union of two sets A (1 2 3 4) and 
B (3 4 5 6 7) is (1 2 3 4 5 6 7).
Difference A-B is 1 2.
The intersection is 3 4.

Your Task:
You do not need to read input or print anything. Your task is to complete the function setOperations() which takes set A and set B as input parameters and returns a list containing the three desired sets.

Expected Time Complexity: O(N + M)
Expected Auxiliary Space: O(N+M)

Constraints:
1 <= N, M <= 1000
0
bickyjha0552 months ago
class Solution{   public:   vector<set<int>> setOperations(set<int> A, set<int> B){       // code here       vector <set<int> > ans;        vector<int > temp_v;        set <int> temp_s;        //inbuilt funtion to find union        set_union(A.begin(),A.end(),B.begin(),B.end(),back_inserter(temp_v));        // vector main set push kr do        temp_s.insert(temp_v.begin(),temp_v.end());        //  vector ko ans main push kr do        ans.emplace_back(temp_s);        // vector or set ko khali kr do        temp_v.clear();        temp_s.clear();                // find difference        set_difference(A.begin(),A.end(),B.begin(),B.end(),back_inserter(temp_v));                if(temp_v.empty()){            temp_v.emplace_back(0);        }         //set main insert kr do          temp_s.insert(temp_v.begin(), temp_v.end());                    ans.emplace_back(temp_s);                    temp_v.clear();          temp_s.clear();                    // intersection         set_intersection(A.begin(), A.end(),B.begin(), B.end(),back_inserter(temp_v));                      if(temp_v.empty())       {           temp_v.emplace_back(0);       }              temp_s.insert(temp_v.begin(), temp_v.end());       ans.emplace_back(temp_s);              temp_v.clear();       temp_s.clear();              return ans;   }};
+2
Debojyoti Sinha9 months ago
Debojyoti Sinha
Problem Solved Successfully ✅Total Time Taken: 0.3/1.6
class Solution{    public:    vector<set<int>> setOperations(set<int> A, set<int> B)    {        vector<set<int>> res;                vector<int> tempVec;        set<int> tempS;                set_union(            A.begin(), A.end(),            B.begin(), B.end(),            back_inserter(tempVec)        );                tempS.insert(tempVec.begin(), tempVec.end());        res.emplace_back(tempS);                tempVec.clear();        tempS.clear();                set_difference(            A.begin(), A.end(),            B.begin(), B.end(),            back_inserter(tempVec)        );                if(tempVec.empty())        {            tempVec.emplace_back(0);        }                tempS.insert(tempVec.begin(), tempVec.end());        res.emplace_back(tempS);                tempVec.clear();        tempS.clear();                set_intersection(            A.begin(), A.end(),            B.begin(), B.end(),            back_inserter(tempVec)        );                if(tempVec.empty())        {            tempVec.emplace_back(0);        }                tempS.insert(tempVec.begin(), tempVec.end());        res.emplace_back(tempS);                tempVec.clear();        tempS.clear();                return res;    }};
0
Sparsh Malhotra2 years ago
Sparsh Malhotra
0
shreesha mg2 years ago
shreesha mg
#include <bits stdc++.h="">using namespace std;
int main() {//codeint t;cin>>t;while(t--){    int m,n;    cin >> m >> n;    set<int> s1,s2;    for(int i=0;i<m;i++) {="" int="" x;="" cin="">> x;        s1.insert(x);    }    for(int i=0;i<n;i++) {="" int="" x;="" cin="">> x;        s2.insert(x);    }    set<int> s3,s4,s5;    std::set<int>::iterator it1,it2,it3,a,b,c;     set_union(s1.begin(),s1.end(),s2.begin(),s2.end(),inserter(s3, s3.begin()));     set_difference(s1.begin(),s1.end(),s2.begin(),s2.end(),inserter(s4, s4.begin()));     set_intersection(s1.begin(),s1.end(),s2.begin(),s2.end(),inserter(s5, s5.begin()));
    for( a=s3.begin();a!=s3.end();++a)    {        cout << *a <<" ";    }     if(s3.size()==0) cout << "0";    cout<< endl;    for( b=s4.begin();b!=s4.end();++b)    {        cout << *b <<" ";    }    if(s4.size()==0) cout << "0";    cout<< endl;    for( b=s5.begin();b!=s5.end();++b)    {        cout << *b <<" ";    }     if(s5.size()==0) cout << "0";    cout<< endl;}return 0;}
Please upvote if it helped
0
infinity3 years ago
infinity
https://ide.geeksforgeeks.o...
0
VINIT SHAHDEO4 years ago
VINIT SHAHDEO
Why this code is giving time complexity error?
#include<bits stdc++.h="">using namespace std;int main() {int t;cin>>t;while(t--){    int n,m;    set<int> s1,s2,s3;    int x;    for(int i=0;i<n;i++) {="" cin="">>x;        s1.insert(x);        s3.insert(x);    }    for(int i=0;i<m;i++) {="" cin="">>x;        s2.insert(x);        s3.insert(x);    }    set<int>::iterator it;    for(it=s3.begin();it!=s3.end();it++)    {        cout<<*it<<" ";    }    cout<<endl; int="" flag="0;" for(it="s1.begin();it!=s1.end();it++)" {="" cout<<*it<<endl;="" if(s2.find(*it)!="s2.end())" {="" flag="1;" cout<<*it<<"="" ";="" }="" }="" if(!flag)="" cout<<"0";="" cout<<endl;="" flag="0;" for(it="s1.begin();it!=s1.end();it++)" {="" if(s2.find(*it)!="s2.end())" {="" flag="1;" cout<<*it<<"="" ";="" }="" }="" if(!flag)="" cout<<"0";="" cout<<endl;="" }="" return="" 0;="" }="">
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.