Search an Element in a Sorted Array
Given a sorted array 'A' of 'N' integers, determine whether a number 'X' exists within this array for a series of queries. For each query, print 1 if 'X' exists in the array, otherwise print 0.
Input:
The first line contains an integer 'T', the number of test cases. For each test case, the following inputs are provided:
1. An integer 'N', the size of the array 'A'.
2. 'N' space-separated integers, the sorted elements of the array 'A'.
3. An integer 'Q', the number of queries.
4. 'Q' integers, each representing a query integer 'X'.
Output:
For each test case, output 'Q' space-separated integers, each being 1 or 0 depending on whether the corresponding query integer 'X' exists in array 'A'. Each test case's output should be on a new line.
Example:
Input:
T = 1
N = 5
A = [1, 2, 3, 4, 5]
Q = 3
X queries = [3, 10, 4]
Output:
1 0 1
Constraints:
1 <= T <= 10
1 <= N <= 10^5
-10^9 <= A[i] <= 10^9
1 <= Q <= 10^4
-10^9 <= X <= 10^9
- The array 'A' is sorted in non-decreasing order.
Note:
You do not need to handle any I/O operations manually.

AnswerBot
4mo
Search for a number in a sorted array and determine its existence for multiple queries.
Iterate through each query integer 'X' and perform binary search on the sorted array 'A' to check for its existen...read more
Help your peers!
Add answer anonymously...
TCS Associate Software Engineer interview questions & answers
An Associate Software Engineer was asked 2w agoQ. What is IPL in Mainframe?
An Associate Software Engineer was asked 2mo agoQ. What is abstraction?
An Associate Software Engineer was asked 2mo agoQ. What is inheritance?
Popular interview questions of Associate Software Engineer
An Associate Software Engineer was asked 2w agoQ1. What is IPL in Mainframe?
An Associate Software Engineer was asked 2mo agoQ2. What is abstraction?
An Associate Software Engineer was asked 2mo agoQ3. What is inheritance?
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

