Find whether an array is subset of another array O(n).
Method 2
#include<stdio.h>
#include <iostream>
#include <set>
#include<iterator>
using namespace std;
int main ()
{
std::set<int>myset;
std::set<int>::iterator it;
int arr[7]={1,2,3,4,5,6,7};
int arr2[3]={1,2,4};
int i;
int temp=true;
for(i=0;i<7;i++)
{
myset.insert(arr[i]);
}
for(i=0;i<3;i++)
{
it=myset.find(arr2[i]);
if(*it==arr[6])
{
printf("No");
temp=false;
break;
}
}
if(temp==true)
{
printf("Yes this is sub array \n");
}
return 0;
}
No comments:
Post a Comment