Find Position of First One

Given a sorted array of integers of size N, consisting only of 0's and 1's, identify the position of the first occurrence of '1', using 1-based indexing.

If the array contains only 0's with no '1's, return -1.

Input:

The first line contains an integer 't', denoting the number of test cases. 
Each test case consists of:
- An integer N, the size of the array.
- N space-separated integers of only ‘0’s and ‘1’s in sorted order.

Output:

For each test case, output the position of the first ‘1’. If no ‘1’ is present, print “-1”. Each test case should be output on a new line.

Example:

Input:
t = 2
N = 5
Array = [0, 0, 0, 1, 1]
N = 3
Array = [0, 0, 0]
Output:
4
-1

Constraints:

  • 1 ≤ t ≤ 100
  • 0 ≤ N ≤ 104

Note: Implement the function without printing anything directly, as the output handling is managed elsewhere.

AnswerBot
10d

Find the position of the first occurrence of '1' in a sorted array of 0's and 1's.

  • Iterate through the array and find the index of the first '1'.

  • Return the index + 1 as the position (1-based indexing)....read more

Help your peers!
Add answer anonymously...
TCS System 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