
Asked in TCS
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...
Top Associate Software Engineer Interview Questions Asked at TCS
Q. What is IPL in Mainframe?
Q. What is abstraction?
Q. What is inheritance?
Interview Questions Asked to Associate Software Engineer at Other Companies
Top Skill-Based Questions for TCS Associate Software Engineer
Data Structures Interview Questions and Answers
250 Questions
Algorithms Interview Questions and Answers
250 Questions
Java Interview Questions and Answers
250 Questions
SQL Interview Questions and Answers
250 Questions
Web Development Interview Questions and Answers
250 Questions
C++ Interview Questions and Answers
150 Questions
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

