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
1mo

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
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

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