# People Whose List of Favorite Companies Is Not a Subset of Another List in C++

C++Server Side ProgrammingProgramming

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.

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 −

• 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 −

Live Demo

#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;
}
{{"TCS","google","facebook"},{"google","microsoft"},{"google","facebook"},{"google"},{"amazon"}}
[0, 1, 4, ]