找到必须设置位以最大化下一个设置位之间的距离的索引
给定一个二进制数组arr[] 。任务是在arr[]中找到任何0的位置,以使两个设置位之间的距离最大化。
例子
Input: arr = [1, 0, 0, 0, 1, 0, 1]
Output: 2
Explanation: Flip the bit at arr[2]
Input: arr = [1, 0, 0, 0]
Output: 3
方法:这个问题可以通过找到一些变化的相邻设置位之间的最长距离来解决。请按照以下步骤解决给定的问题。
- 对于相邻设置位之间的所有距离,找到最大的一个并将其一半存储为所需答案之一。
- 然后找到0和第一个设置位之间的距离,以及索引N-1和最后一个设置位之间的距离。
- 找到总体最大值作为所需答案。
- 打印最后找到的答案。
下面是上述方法的实现。
C++
// C++ program for above approach
#include
using namespace std;
// Function to find the maximum distance between any
// two set bits after flipping one bit
int maxDistToClosest1(vector& arr)
{
// The size of the array
int n = arr.size(), ans = 0;
int temp = 1, setbit = 0;
// Iterate through the array
for (int i = 1; i < n; i++) {
if (arr[i] == 1) {
if (setbit == 0 && arr[0] == 0)
ans = max(ans, temp);
else
ans = max(ans, temp / 2);
setbit = 1;
temp = 0;
}
temp++;
}
ans = arr[n - 1] == 0 ? max(temp - 1, ans)
: max(temp / 2, ans);
// Return the answer found
return ans;
}
// Driver Code
int main()
{
vector arr = { 1, 0, 0, 0, 1, 0, 1 };
// Function Call
cout << maxDistToClosest1(arr);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to find the maximum distance between any
// two set bits after flipping one bit
static int maxDistToClosest1(int arr[])
{
// The size of the array
int n = arr.length, ans = 0;
int temp = 1, setbit = 0;
// Iterate through the array
for (int i = 1; i < n; i++) {
if (arr[i] == 1) {
if (setbit == 0 && arr[0] == 0)
ans = Math.max(ans, temp);
else
ans = Math.max(ans, temp / 2);
setbit = 1;
temp = 0;
}
temp++;
}
ans = arr[n - 1] == 0 ? Math.max(temp - 1, ans)
: Math.max(temp / 2, ans);
// Return the answer found
return ans;
}
// Driver Code
public static void main (String[] args) {
int arr[] = { 1, 0, 0, 0, 1, 0, 1 };
// Function Call
System.out.print(maxDistToClosest1(arr));
}
}
// This code is contributed by hrithikgarg03188.
Python
# Pyhton program for above approach
# Function to find the maximum distance between any
# two set bits after flipping one bit
def maxDistToClosest1(arr):
# The size of the array
n = len(arr)
ans = 0
temp = 1
setbit = 0
# Iterate through the array
for i in range(1, n):
if (arr[i] == 1):
if (setbit == 0 and arr[0] == 0):
ans = max(ans, temp)
else:
ans = max(ans, temp // 2)
setbit = 1
temp = 0
temp +=1
if(arr[n - 1] == 0):
ans = max(temp - 1, ans)
else:
ans = max(temp // 2, ans)
# Return the answer found
return ans
# Driver Code
arr = [ 1, 0, 0, 0, 1, 0, 1 ]
# Function Call
print(maxDistToClosest1(arr))
# This code is contributed by Samim Hossain Mondal.
C#
// C# program for above approach
using System;
class GFG
{
// Function to find the maximum distance between any
// two set bits after flipping one bit
static int maxDistToClosest1(int[] arr)
{
// The size of the array
int n = arr.Length, ans = 0;
int temp = 1, setbit = 0;
// Iterate through the array
for (int i = 1; i < n; i++) {
if (arr[i] == 1) {
if (setbit == 0 && arr[0] == 0)
ans = Math.Max(ans, temp);
else
ans = Math.Max(ans, temp / 2);
setbit = 1;
temp = 0;
}
temp++;
}
ans = arr[n - 1] == 0 ? Math.Max(temp - 1, ans)
: Math.Max(temp / 2, ans);
// Return the answer found
return ans;
}
// Driver Code
public static int Main()
{
int[] arr = { 1, 0, 0, 0, 1, 0, 1 };
// Function Call
Console.Write(maxDistToClosest1(arr));
return 0;
}
}
// This code is contributed by Taranpreet
Javascript
输出
2
时间复杂度: O(N)
辅助空间: O(1)