Search an Element in an Array

You have given a sorted array 'A' of 'N' integers.

Now, you are given 'Q' queries, and each query consists of a single integer 'X'. Your task is to check whether 'X' is present in array 'A' or not for each query. If 'X' exists in array 'A', you need to print 1 else print 0.

Note :

The given array is sorted in non-decreasing order. 
Input format:
The first line of the input contains an integer 'T' representing the number of test cases or queries to be processed. Then the 'T' test case follows. 

The first line of each test case contains a single integer 'N' denoting the size of the array 'A'.

The second line of each test case contains 'N' space-separated integers denoting the elements of the array 'A'.

The third line of each test contains a single integer 'Q', denoting the number of queries. 

Then each of the 'Q' lines from the fourth line of each test case contains a single integer 'X', denoting the desired element to be searched.
Output Format:
For each test case, print 'Q' space-separated integers that denote the answer to the given 'Q' queries, i.e., print 1 if the desired value 'X' exists in the array 'A', otherwise print 0.

Print the answer for each test case in a new line.
Note:
You are not required to print anything, it has already been taken care of. Just implement the function.
Constraints:
1 <= T <= 10 
1 <= N <= 10^5    
-10^9 <= A[i] <= 10^9 
1 <= Q <= 10^4
-10^9 <= X <= 10^9

Where 'T' represents the number of test cases, 'N' represents the size of the array, 'A[i]' represents the elements of the array, 'Q' represents the number of queries and, 'X' denotes the number to be searched.
Time limit: 1 sec
CodingNinjas
author
2y

Step 1 : Pen and paper approach. Explain the problem statement first and then give them an algo.
Step 2 : He asked me to implement linear search
Step 3 : Asked about the time complexity of both linear a...read more

CodingNinjas
author
2y
Brute Force

The idea here is to do a linear search which apparently is a brute force way, so for each query:

  1. Visit every element one by one.
  2. Check if the current element is equal to X or not. If yes, the...read more
CodingNinjas
author
2y
Recursive binary search

We will use a recursive binary search to solve this problem. The idea of binary search is to discard half intervals by just one comparison.

Let's say A[mid] = middle element of ...read more

CodingNinjas
author
2y
Iterative binary search

Instead of recursive binary search, we will use iterative binary search to check if the element in the array is present or not.

Steps:

  1. For each query, we do a binary search on an ...read more
Add answer anonymously...
TCS Associate Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter