Given a sorted array consisting 0's and 1's. The problem is to find the index of first '1' in the sorted array. It could be possible that the array consists of only 0's or only 1's. If 1's are not present in the array then print "-1".
Examples :
Input : arr[] = {0, 0, 0, 0, 0, 0, 1, 1, 1, 1}
Output : 6
Explanation: The index of first 1 in the array is 6.Input : arr[] = {0, 0, 0, 0}
Output : -1
Explanation: 1's are not present in the array.
Try It Yourself
Table of Content
[Naive Approach] Using Linear Search - O(n) Time O(1) Space
Traverse the array from left to right and return the index of first '1'. If 1's are not present in the array, then print "-1"
// C++ implementation to find the index of
// first '1' in a sorted array of 0's and 1's
#include <bits/stdc++.h>
using namespace std;
// function to find the index of first '1'
int firstIndex(vector<int> &arr)
{
int n = arr.size(); // fix: define n
for (int i = 0; i < n; i++)
if (arr[i] == 1)
return i;
return -1;
}
// Driver Code
int main()
{
vector<int> arr = { 0, 0, 0, 0, 0, 0, 1, 1, 1, 1 };
cout << firstIndex(arr);
return 0;
}
// Java implementation to find the index of
// first '1' in a sorted array of 0's and 1's
public class GfG {
// function to find the index of first '1'
static int firstIndex(int[] arr) {
int n = arr.length; // fix: define n
for (int i = 0; i < n; i++)
if (arr[i] == 1)
return i;
return -1;
}
// Driver Code
public static void main(String[] args) {
int[] arr = { 0, 0, 0, 0, 0, 0, 1, 1, 1, 1 };
System.out.println(firstIndex(arr));
}
}
# Python3 implementation to find the index of
# first '1' in a sorted array of 0's and 1's
# function to find the index of first '1'
def firstIndex(arr):
n = len(arr)
for i in range(n):
if arr[i] == 1:
return i
return -1
#Driver Code
arr = [0, 0, 0, 0, 0, 0, 1, 1, 1, 1]
print(firstIndex(arr))
// C# implementation to find the index of
// first '1' in a sorted array of 0's and 1's
using System;
using System.Collections.Generic;
public class GfG
{
// function to find the index of first '1'
public static int firstIndex(List<int> arr)
{
int n = arr.Count;
for (int i = 0; i < n; i++)
if (arr[i] == 1)
return i;
return -1;
}
// Driver Code
public static void Main()
{
List<int> arr = new List<int> { 0, 0, 0, 0, 0, 0, 1, 1, 1, 1 };
Console.WriteLine(firstIndex(arr));
}
}
// JavaScript implementation to find the index of
// first '1' in a sorted array of 0's and 1's
// function to find the index of first '1'
function firstIndex(arr) {
let n = arr.length;
for (let i = 0; i < n; i++)
if (arr[i] === 1)
return i;
return -1;
}
//Driver Code
let arr = [0, 0, 0, 0, 0, 0, 1, 1, 1, 1];
console.log(firstIndex(arr));
Output
6
[Efficient Approach] Using Binary Search - O(Log n)Time O(1) Space
Use the technique of binary search on the sorted array, so as to find the index of first '1'.
Algorithm:
- Initialize
low = 0andhigh = n - 1. - Find
mid = (low + high) / 2and check the value atmid. - If
arr[mid] == 1and it is the first occurrence, returnmid. - If
arr[mid] == 1, move left (high = mid - 1); else move right (low = mid + 1). - Repeat until found; if not, return
-1.
// C++ implementation to find the index of first
// '1' in a sorted array of 0's and 1's
#include <bits/stdc++.h>
using namespace std;
// function to find the index of first '1'
// using binary search technique
int firstIndex(vector<int> &arr)
{
int low = 0;
int high = arr.size() - 1;
// iterate while search space is valid
while (low <= high)
{
int mid = (low + high) / 2;
// check if mid is the first occurrence of '1'
if (arr[mid] == 1 && (mid == 0 || arr[mid - 1] == 0))
return mid;
// if '1' is found but not first, move to left half
else if (arr[mid] == 1)
high = mid - 1;
// if '0' is found, move to right half
else
low = mid + 1;
}
// if '1' is not present in the array
return -1;
}
// Driver program to test above function
int main()
{
vector<int> arr = {0, 0, 0, 0, 0, 0, 1, 1, 1, 1};
// function call
cout << firstIndex(arr);
return 0;
}
import java.util.*;
public class GfG {
// function to find the index of first '1'
// using binary search technique
public static int firstIndex(int[] arr) {
int low = 0;
int high = arr.length - 1;
// iterate while search space is valid
while (low <= high) {
int mid = (low + high) / 2;
// check if mid is the first occurrence of '1'
if (arr[mid] == 1 && (mid == 0 || arr[mid - 1] == 0))
return mid;
// if '1' is found but not first, move to left half
else if (arr[mid] == 1)
high = mid - 1;
// if '0' is found, move to right half
else
low = mid + 1;
}
// if '1' is not present in the array
return -1;
}
public static void main(String[] args) {
int[] arr = {0, 0, 0, 0, 0, 0, 1, 1, 1, 1};
// function call
System.out.println(firstIndex(arr));
}
}
def firstIndex(arr):
low = 0
high = len(arr) - 1
# iterate while search space is valid
while low <= high:
mid = (low + high) // 2
# check if mid is the first occurrence of '1'
if arr[mid] == 1 and (mid == 0 or arr[mid - 1] == 0):
return mid
# if '1' is found but not first, move to left half
elif arr[mid] == 1:
high = mid - 1
# if '0' is found, move to right half
else:
low = mid + 1
# if '1' is not present in the array
return -1
# Driver program to test above function
arr = [0, 0, 0, 0, 0, 0, 1, 1, 1, 1]
# function call
print(firstIndex(arr))
using System;
public class GfG
{
// function to find the index of first '1'
// using binary search technique
public static int firstIndex(int[] arr)
{
int low = 0;
int high = arr.Length - 1;
// iterate while search space is valid
while (low <= high)
{
int mid = (low + high) / 2;
// check if mid is the first occurrence of '1'
if (arr[mid] == 1 && (mid == 0 || arr[mid - 1] == 0))
return mid;
// if '1' is found but not first, move to left half
else if (arr[mid] == 1)
high = mid - 1;
// if '0' is found, move to right half
else
low = mid + 1;
}
// if '1' is not present in the array
return -1;
}
public static void Main()
{
int[] arr = {0, 0, 0, 0, 0, 0, 1, 1, 1, 1};
// function call
Console.WriteLine(firstIndex(arr));
}
}
// JavaScript implementation to find the index of first
// '1' in a sorted array of 0's and 1's
// function to find the index of first '1'
// using binary search technique
function firstIndex(arr) {
let low = 0;
let high = arr.length - 1;
// iterate while search space is valid
while (low <= high) {
let mid = Math.floor((low + high) / 2);
// check if mid is the first occurrence of '1'
if (arr[mid] == 1 && (mid == 0 || arr[mid - 1] == 0))
return mid;
// if '1' is found but not first, move to left half
else if (arr[mid] == 1)
high = mid - 1;
// if '0' is found, move to right half
else
low = mid + 1;
}
// if '1' is not present in the array
return -1;
}
// Driver program to test above function
let arr = [0, 0, 0, 0, 0, 0, 1, 1, 1, 1];
// function call
console.log(firstIndex(arr));
Output
6