Array Game

QUESTION

You are given an array A of size N, and Q queries to deal with. For each query, you are given an integer X, and you’re supposed to find out if X is present in the array A or not.\n\nInput:\nThe first line contains two integers, N and Q, denoting the size of array A and number of queries. The second line contains N space separated integers, denoting the array of elements Ai. The next Q lines contain a single integer X per line.\n\nOutput:\nFor each query, print YES if the X is in the array, otherwise print NO.

“TESTCASE_1”: “5 10\n50 40 30 20 10\n10\n20\n30\n40\n50\n60\n70\n80\n90\n100\n###—###SEPERATOR—###—\nYES\nYES\nYES\nYES\nYES\nNO\nNO\nNO\nNO\nNO”, “TESTCASE_2”: “4 12\n47 36 21 12\n12\n21\n36\n47\n52\n69\n75\n88\n91\n100\n122\n140\n###—###SEPERATOR—###—\nYES\nYES\nYES\nYES\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO”, “TESTCASE_3”: “5 8\n42 31 29 12 2\n2\n12\n29\n31\n42\n50\n60\n99\n###—###SEPERATOR—###—\nYES\nYES\nYES\nYES\nYES\nNO\nNO\nNO”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include <stdio.h>
int main()
{int n,q;
 scanf("%d%d",&n,&q);
 int a[n],i;
 for(i=0;i<n;i++)
 {
   scanf("%d",&a[i]);
 }
 int q1;
 while(q--)
 {
   scanf("%d",&q1);
   int flag=0;
   for(i=0;i<n;i++)
   {
    if(a[i]==q1)
    {
      flag=1;
      break;
    }
   }
   if(flag==1)
     printf("YES\n");
   else
     printf("NO\n");
 }
	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