Subset AND

QUESTION

You are given a number Z and a set S with N elements. Your job is to find a sub set of S such that the AND of the given number and this subset is zero. If this sub set is possible print \”Yes\” otherwise print \”No\”\n\nInput\n\nFirst line contains number of test case T. Each test case contains two lines , first line contains two numbers Z and N , where Z is given number and N is size of set S . \n\nSecond line contains N elements of the subset S.\n\nOutput\n\nFor each test case print Yes if the subset is possible else print No .\n\nConstraints:\n1<=T<=100\n1<=N<=1000\n0<=Ai<=Z<=1000000.

ANSWER

#include <bits/stdc++.h>
using namespace std;
int main()
{
	int t, n, z, i, j, temp, ans;
	cin>>t;
	while(t--)
	{
		cin>>z>>n;
		for(i=0; i<n; i++)
		{
			cin>>temp;
			if(i!=0)
			{
				ans=(ans&temp);
			}
			else
			{
				ans=temp;
			}
		}
		if((ans&z)==0)
		{
			cout<<"Yes"<<endl;
		}
		else
		{
			cout<<"No"<<endl;
		}
	}
	return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
CHP Adblock Detector Plugin | Codehelppro