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!
Select
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?
Associate Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
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

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits