- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
People Whose List of Favorite Companies Is Not a Subset of Another List in C++
Suppose we have an array called favorite companies where favoriteCompanies[i] is the list of favorites companies of the ith person. We have to find the indices of people whose list of favorite companies is not a subset of any other list of favorites companies.
So, if the input is like favoriteCompanies = [["TCS", "google", "facebook"], ["google","microsoft"], ["google", "facebook"], ["google"], ["amazon"]], then the output will be [0,1,4], this is because person with index=2 has ["google", "facebook"] which is a subset of favoriteCompanies[0]= ["TCS", "google", "facebook"] corresponding to the person with index 0.
Now person with index=3 has ["google"] which is a subset of favoriteCompanies[0]= ["TCS", "google", "facebook"] and favoriteCompanies[1]= ["google", "microsoft"]. Other lists of favorite companies are not a subset of another list, so, the answer is [0,1,4].
To solve this, we will follow these steps −
Define a function ok(), this will take an array a, an array b,
cnt := 0, i := 0, j := 0
while (i < size of a and j < size of b), do −
if a[i] is same as b[j], then −
(increase i, j and cnt by 1)
otherwise when a[i] < b[j], then −
(increase i by 1)
Otherwise
(increase j by 1)
return true when cnt < size of a
From the main method do the following −
Define one set s
n := size of f
for initialize i := 0, when i < n, update (increase i by 1), do −
sort the array f[i]
for initialize i := 0, when i < n, update (increase i by 1), do −
c := true
for initialize j := 0, when j < n, update (increase j by 1), do −
if i is same as j, then −
Ignore the following part, skip to the next iteration
c := c AND ok(f[i], f[j])
if c is non-zero, then −
insert i into s
return elements of s as an array
Example
Let us see the following implementation to get a better understanding −
#include <bits/stdc++.h> using namespace std; void print_vector(vector<int> v){ cout << "["; for(int i = 0; i<v.size(); i++){ cout << v[i] << ", "; } cout << "]"<<endl; } class Solution { public: bool ok(vector<string>& a, vector<string>& b){ int cnt = 0; int i = 0; int j = 0; while (i < a.size() && j < b.size()) { if (a[i] == b[j]) { i++; j++; cnt++; } else if (a[i] < b[j]) { i++; } else { j++; } } return cnt < a.size(); } vector<int> peopleIndexes(vector<vector<string> >& f){ set<int> s; int n = f.size(); for (int i = 0; i < n; i++) { sort(f[i].begin(), f[i].end()); } for (int i = 0; i < n; i++) { bool c = true; for (int j = 0; j < n; j++) { if (i == j) continue; c &= ok(f[i], f[j]); } if (c) s.insert(i); } return vector<int>(s.begin(), s.end()); } }; main(){ Solution ob; vector<vector<string>> v = {{"TCS","google","facebook"},{"google","microsoft"},{"google","facebo ok"},{"google"},{"amazon"}}; print_vector(ob.peopleIndexes(v)); }
Input
{{"TCS","google","facebook"},{"google","microsoft"},{"google","facebook"},{"google"},{"amazon"}}
Output
[0, 1, 4, ]