查找二进制数组中的过渡点
给定一个仅包含数字 0 和 1 的排序数组,任务是有效地找到转换点。过渡点是“0”结束和“1”开始的点。
例子 :
Input: 0 0 0 1 1
Output: 3
Explanation: Index of first 1 is 3
Input: 0 0 0 0 1 1 1 1
Output: 4
Explanation: Index of first 1 is 4
Naive Approach:遍历数组并打印第一个1的索引。
- 从数组的开始到结束遍历数组。
- 如果当前元素为 1,则打印索引并终止程序。
下面是上述方法的实现:
C++
// C++ implementation to find
// the transition point
#include
using namespace std;
// Function to find the transition point
int findTransitionPoint(int arr[], int n)
{
//perform a linear search and
// return the index of
//first 1
for(int i=0; i= 0 ? cout << "Transition point is "
<< point
: cout<<"There is no transition point";
return 0;
}
Java
// Java implementation to find the transition point
import java.util.*;
class GFG
{
// Function to find the transition point
static int findTransitionPoint(int arr[], int n)
{
// perform a linear search and return the index of
// first 1
for(int i = 0; i < n ; i++)
if(arr[i] == 1)
return i;
// if no element is 1
return -1;
}
// Driver code
public static void main (String[] args)
{
int arr[] = {0, 0, 0, 0, 1, 1};
int n = arr.length;
int point = findTransitionPoint(arr, n);
if (point >= 0)
System.out.print("Transition point is " + point);
else
System.out.print("There is no transition point");
}
}
// This code is contributed by shivanisinghss2110
Python3
# Python3 implementation to find the transition point
# Function to find the transition point
def findTransitionPoint(arr, n):
# perform a linear search and return the index of
# first 1
for i in range(n):
if(arr[i] == 1):
return i
# if no element is 1
return -1
# Driver code
arr = [0, 0, 0, 0, 1, 1]
n = len(arr)
point = findTransitionPoint(arr, n)
if point >= 0:
print("Transition point is", point)
else:
print("There is no transition point")
# This code is contributed by shubhamsingh10
C#
// C# implementation to find the transition point
using System;
class GFG
{
// Function to find the transition point
static int findTransitionPoint(int []arr ,int n)
{
// perform a linear search and return the index of
// first 1
for(int i = 0; i < n ; i++)
if(arr[i] == 1)
return i;
// if no element is 1
return -1;
}
// Driver method
public static void Main()
{
int []arr = {0, 0, 0, 0, 1, 1};
int point = findTransitionPoint(arr, arr.Length);
Console.Write(point >= 0 ? "Transition point is " +
point : "There is no transition point");
}
}
// This code is contributed by shivanisinghss2110
Javascript
C++
// C++ implementation to find the transition point
#include
using namespace std;
// Function to find the transition point
int findTransitionPoint(int arr[], int n)
{
// Initialise lower and upper bounnds
int lb = 0, ub = n-1;
// Perform Binary search
while (lb <= ub)
{
// Find mid
int mid = (lb+ub)/2;
// update lower_bound if mid contains 0
if (arr[mid] == 0)
lb = mid+1;
// If mid contains 1
else if (arr[mid] == 1)
{
// Check if it is the left most 1
// Return mid, if yes
if (mid == 0
|| (mid > 0 &&
arr[mid - 1] == 0))
return mid;
// Else update upper_bound
ub = mid-1;
}
}
return -1;
}
// Driver Code
int main()
{
int arr[] = {0, 0, 0, 0, 1, 1};
int n = sizeof(arr) / sizeof(arr[0]);
int point = findTransitionPoint(arr, n);
point >= 0 ? cout<<"Transition point is " << point
: cout<<"There is no transition point";
return 0;
}
Java
// Java implementation to find the transition point
class Test {
// Method to find the transition point
static int findTransitionPoint(int arr[], int n)
{
// Initialise lower and upper bounnds
int lb = 0, ub = n - 1;
// Perform Binary search
while (lb <= ub) {
// Find mid
int mid = (lb + ub) / 2;
// update lower_bound if mid contains 0
if (arr[mid] == 0)
lb = mid + 1;
// If mid contains 1
else if (arr[mid] == 1) {
// Check if it is the left most 1
// Return mid, if yes
if (mid == 0
|| (mid > 0 &&
arr[mid - 1] == 0))
return mid;
// Else update upper_bound
ub = mid - 1;
}
}
return -1;
}
// Driver method
public static void main(String args[])
{
int arr[] = { 0, 0, 0, 0, 1, 1 };
int point = findTransitionPoint(arr, arr.length);
System.out.println(
point >= 0 ? "Transition point is " + point
: "There is no transition point");
}
}
Python3
# python implementation to find the
# transition point
# Function to find the transition
# point
def findTransitionPoint(arr, n):
# Initialise lower and upper
# bounnds
lb = 0
ub = n - 1
# Perform Binary search
while (lb <= ub):
# Find mid
mid = (int)((lb + ub) / 2)
# update lower_bound if
# mid contains 0
if (arr[mid] == 0):
lb = mid + 1
# If mid contains 1
else if (arr[mid] == 1):
# Check if it is the
# left most 1 Return
# mid, if yes
if (mid == 0 \
or (mid > 0 and\
arr[mid - 1] == 0)):
return mid
# Else update
# upper_bound
ub = mid-1
return -1
# Driver code
arr = [0, 0, 0, 0, 1, 1]
n = len(arr)
point = findTransitionPoint(arr, n);
if(point >= 0):
print("Transition point is ", point)
else:
print("There is no transition point")
# This code is contributed by Sam007
C#
// C# implementation to find the transition point
using System;
class GFG
{
// Method to find the transition point
static int findTransitionPoint(int []arr, int n)
{
// Initialise lower and upper bounnds
int lb = 0, ub = n-1;
// Perform Binary search
while (lb <= ub)
{
// Find mid
int mid = (lb+ub)/2;
// update lower_bound if mid contains 0
if (arr[mid] == 0)
lb = mid+1;
// If mid contains 1
else if (arr[mid] == 1)
{
// Check if it is the left most 1
// Return mid, if yes
if (mid == 0
|| (mid > 0 &&
arr[mid - 1] == 0))
return mid;
// Else update upper_bound
ub = mid-1;
}
}
return -1;
}
// Driver method
public static void Main()
{
int []arr = {0, 0, 0, 0, 1, 1};
int point = findTransitionPoint(arr, arr.Length);
Console.Write(point >= 0 ? "Transition point is " +
point : "There is no transition point");
}
}
// This code is contributed by Sam007
PHP
0 and
$arr[$mid - 1] == 0))
return $mid;
// Else update upper_bound
$ub = $mid - 1;
}
}
return -1;
}
// Driver Code
$arr = array(0, 0, 0, 0, 1, 1);
$n = sizeof($arr);
$point = findTransitionPoint($arr, $n);
if($point >= 0)
echo "Transition point is " , $point;
else
echo"There is no transition point";
return 0;
// This code is contributed by nitin mittal.
?>
Javascript
输出
Transition point is 4
复杂性分析:
- 时间复杂度: O(n),只需要遍历一次,所以时间复杂度是O(n)
- 辅助空间: O(1),不需要额外的空间。
高效方法:思路是使用二分查找,在数组中找到1的最小索引。随着数组的排序,可以执行二进制搜索。
- 创建两个变量l和r ,初始化l = 0和r = n-1和一个变量ans = -1来存储答案。
- 重复以下步骤直到l <= r ,下限小于上限。
- 检查中间索引mid = (l+r)/2处的元素是否为 1。
- 如果元素为1,则检查中间元素左侧的1 个元素的最小索引,即更新r = mid – 1和更新ans = mid 。
- 如果元素为零,则检查中间元素右侧的 1 个元素的最小索引,即更新l = mid + 1 。
下面是上述方法的实现:
C++
// C++ implementation to find the transition point
#include
using namespace std;
// Function to find the transition point
int findTransitionPoint(int arr[], int n)
{
// Initialise lower and upper bounnds
int lb = 0, ub = n-1;
// Perform Binary search
while (lb <= ub)
{
// Find mid
int mid = (lb+ub)/2;
// update lower_bound if mid contains 0
if (arr[mid] == 0)
lb = mid+1;
// If mid contains 1
else if (arr[mid] == 1)
{
// Check if it is the left most 1
// Return mid, if yes
if (mid == 0
|| (mid > 0 &&
arr[mid - 1] == 0))
return mid;
// Else update upper_bound
ub = mid-1;
}
}
return -1;
}
// Driver Code
int main()
{
int arr[] = {0, 0, 0, 0, 1, 1};
int n = sizeof(arr) / sizeof(arr[0]);
int point = findTransitionPoint(arr, n);
point >= 0 ? cout<<"Transition point is " << point
: cout<<"There is no transition point";
return 0;
}
Java
// Java implementation to find the transition point
class Test {
// Method to find the transition point
static int findTransitionPoint(int arr[], int n)
{
// Initialise lower and upper bounnds
int lb = 0, ub = n - 1;
// Perform Binary search
while (lb <= ub) {
// Find mid
int mid = (lb + ub) / 2;
// update lower_bound if mid contains 0
if (arr[mid] == 0)
lb = mid + 1;
// If mid contains 1
else if (arr[mid] == 1) {
// Check if it is the left most 1
// Return mid, if yes
if (mid == 0
|| (mid > 0 &&
arr[mid - 1] == 0))
return mid;
// Else update upper_bound
ub = mid - 1;
}
}
return -1;
}
// Driver method
public static void main(String args[])
{
int arr[] = { 0, 0, 0, 0, 1, 1 };
int point = findTransitionPoint(arr, arr.length);
System.out.println(
point >= 0 ? "Transition point is " + point
: "There is no transition point");
}
}
Python3
# python implementation to find the
# transition point
# Function to find the transition
# point
def findTransitionPoint(arr, n):
# Initialise lower and upper
# bounnds
lb = 0
ub = n - 1
# Perform Binary search
while (lb <= ub):
# Find mid
mid = (int)((lb + ub) / 2)
# update lower_bound if
# mid contains 0
if (arr[mid] == 0):
lb = mid + 1
# If mid contains 1
else if (arr[mid] == 1):
# Check if it is the
# left most 1 Return
# mid, if yes
if (mid == 0 \
or (mid > 0 and\
arr[mid - 1] == 0)):
return mid
# Else update
# upper_bound
ub = mid-1
return -1
# Driver code
arr = [0, 0, 0, 0, 1, 1]
n = len(arr)
point = findTransitionPoint(arr, n);
if(point >= 0):
print("Transition point is ", point)
else:
print("There is no transition point")
# This code is contributed by Sam007
C#
// C# implementation to find the transition point
using System;
class GFG
{
// Method to find the transition point
static int findTransitionPoint(int []arr, int n)
{
// Initialise lower and upper bounnds
int lb = 0, ub = n-1;
// Perform Binary search
while (lb <= ub)
{
// Find mid
int mid = (lb+ub)/2;
// update lower_bound if mid contains 0
if (arr[mid] == 0)
lb = mid+1;
// If mid contains 1
else if (arr[mid] == 1)
{
// Check if it is the left most 1
// Return mid, if yes
if (mid == 0
|| (mid > 0 &&
arr[mid - 1] == 0))
return mid;
// Else update upper_bound
ub = mid-1;
}
}
return -1;
}
// Driver method
public static void Main()
{
int []arr = {0, 0, 0, 0, 1, 1};
int point = findTransitionPoint(arr, arr.Length);
Console.Write(point >= 0 ? "Transition point is " +
point : "There is no transition point");
}
}
// This code is contributed by Sam007
PHP
0 and
$arr[$mid - 1] == 0))
return $mid;
// Else update upper_bound
$ub = $mid - 1;
}
}
return -1;
}
// Driver Code
$arr = array(0, 0, 0, 0, 1, 1);
$n = sizeof($arr);
$point = findTransitionPoint($arr, $n);
if($point >= 0)
echo "Transition point is " , $point;
else
echo"There is no transition point";
return 0;
// This code is contributed by nitin mittal.
?>
Javascript
输出
Transition point is 4
复杂性分析:
- 时间复杂度: O(log n)。二分查找的时间复杂度为 O(log n)。
- 辅助空间: O(1)。不需要额外的空间。