在 0 和 1 的无限排序数组中查找第一个 1 的索引
给定一个由 0 和 1 组成的无限排序数组。问题是在该数组中找到第一个“1”的索引。由于数组是无限的,因此可以保证数组中会出现数字“1”。
例子:
Input : arr[] = {0, 0, 1, 1, 1, 1}
Output : 2
Input : arr[] = {1, 1, 1, 1,, 1, 1}
Output : 0
方法:该问题与在无限数的有序数组中查找元素位置的问题密切相关。由于数组是无限的,因此我们不知道我们必须找到第一个“1”出现的上限和下限。下面是一个找到上限和下限的算法。
算法:
posOfFirstOne(arr)
Declare l = 0, h = 1
while arr[h] == 0
l = h
h = 2*h;
return indexOfFirstOne(arr, l, h)
}
这里h和l是所需的上限和下限。 indexOfFirstOne(arr, l, h)用于查找这两个边界之间第一个“1”的出现索引。参考这篇文章。
C++
// C++ implementation to find the index of first 1
// in an infinite sorted array of 0's and 1's
#include
using namespace std;
// function to find the index of first '1'
// binary search technique is applied
int indexOfFirstOne(int arr[], int low, int high)
{
int mid;
while (low <= high) {
mid = (low + high) / 2;
// if true, then 'mid' is the index of first '1'
if (arr[mid] == 1 &&
(mid == 0 || arr[mid - 1] == 0))
break;
// first '1' lies to the left of 'mid'
else if (arr[mid] == 1)
high = mid - 1;
// first '1' lies to the right of 'mid'
else
low = mid + 1;
}
// required index
return mid;
}
// function to find the index of first 1 in
// an infinite sorted array of 0's and 1's
int posOfFirstOne(int arr[])
{
// find the upper and lower bounds between
// which the first '1' would be present
int l = 0, h = 1;
// as the array is being considered infinite
// therefore 'h' index will always exist in
// the array
while (arr[h] == 0) {
// lower bound
l = h;
// upper bound
h = 2 * h;
}
// required index of first '1'
return indexOfFirstOne(arr, l, h);
}
// Driver program to test above
int main()
{
int arr[] = { 0, 0, 1, 1, 1, 1 };
cout << "Index = "
<< posOfFirstOne(arr);
return 0;
}
Java
// JAVA Code For Find the index of first 1
// in an infinite sorted array of 0s and 1s
import java.util.*;
class GFG {
// function to find the index of first '1'
// binary search technique is applied
public static int indexOfFirstOne(int arr[],
int low, int high)
{
int mid=0;
while (low <= high) {
mid = (low + high) / 2;
// if true, then 'mid' is the index of
// first '1'
if (arr[mid] == 1 &&
(mid == 0 || arr[mid - 1] == 0))
break;
// first '1' lies to the left of 'mid'
else if (arr[mid] == 1)
high = mid - 1;
// first '1' lies to the right of 'mid'
else
low = mid + 1;
}
// required index
return mid;
}
// function to find the index of first 1 in
// an infinite sorted array of 0's and 1's
public static int posOfFirstOne(int arr[])
{
// find the upper and lower bounds
// between which the first '1' would
// be present
int l = 0, h = 1;
// as the array is being considered
// infinite therefore 'h' index will
// always exist in the array
while (arr[h] == 0) {
// lower bound
l = h;
// upper bound
h = 2 * h;
}
// required index of first '1'
return indexOfFirstOne(arr, l, h);
}
/* Driver program to test above function */
public static void main(String[] args)
{
int arr[] = { 0, 0, 1, 1, 1, 1 };
System.out.println("Index = " +
posOfFirstOne(arr));
}
}
// This code is contributed by Arnav Kr. Mandal.
Python3
# Python 3 implementation to find the
# index of first 1 in an infinite
# sorted array of 0's and 1's
# function to find the index of first
# '1' binary search technique is applied
def indexOfFirstOne(arr, low, high) :
while (low <= high) :
mid = (low + high) // 2
# if true, then 'mid' is the index
# of first '1'
if (arr[mid] == 1 and (mid == 0 or
arr[mid - 1] == 0)) :
break
# first '1' lies to the left of 'mid'
elif (arr[mid] == 1) :
high = mid - 1
# first '1' lies to the right of 'mid'
else :
low = mid + 1
# required index
return mid
# function to find the index of first
# 1 in an infinite sorted array of 0's
# and 1's
def posOfFirstOne(arr) :
# find the upper and lower bounds between
# which the first '1' would be present
l = 0
h = 1
# as the array is being considered infinite
# therefore 'h' index will always exist in
# the array
while (arr[h] == 0) :
# lower bound
l = h
# upper bound
h = 2 * h
# required index of first '1'
return indexOfFirstOne(arr, l, h)
# Driver program
arr = [ 0, 0, 1, 1, 1, 1 ]
print( "Index = ", posOfFirstOne(arr))
# This code is contributed by Nikita Tiwari.
C#
// C# Code For Find the index of first 1
// in an infinite sorted array of 0's and 1's
using System;
class GFG {
// function to find the index of first '1'
// binary search technique is applied
public static int indexOfFirstOne(int[] arr,
int low, int high)
{
int mid = 0;
while (low <= high) {
mid = (low + high) / 2;
// if true, then 'mid' is the index of
// first '1'
if (arr[mid] == 1 && (mid == 0 || arr[mid - 1] == 0))
break;
// first '1' lies to the left of 'mid'
else if (arr[mid] == 1)
high = mid - 1;
// first '1' lies to the right of 'mid'
else
low = mid + 1;
}
// required index
return mid;
}
// function to find the index of first 1 in
// an infinite sorted array of 0's and 1's
public static int posOfFirstOne(int[] arr)
{
// find the upper and lower bounds
// between which the first '1' would
// be present
int l = 0, h = 1;
// as the array is being considered
// infinite therefore 'h' index will
// always exist in the array
while (arr[h] == 0) {
// lower bound
l = h;
// upper bound
h = 2 * h;
}
// required index of first '1'
return indexOfFirstOne(arr, l, h);
}
/* Driver program to test above function */
public static void Main()
{
int[] arr = { 0, 0, 1, 1, 1, 1 };
Console.Write("Index = " + posOfFirstOne(arr));
}
}
// This code is contributed by Sam007
PHP
Javascript
输出:
Index = 2