Find the index at which bit must be set to maximize the distance between the next set bit


We are given an array that contains the binary numbers that are '0', and '1' only. We have to make a one-bit set of the given array which was earlier not the set bit (there will be at least a bit present in the given array which will not be a set bit) to set bit such that the number of indexes present in between the set bits of the final array will be at the maximum distance possible.

Sample Examples

Input

int arr[] = {1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1}

Output

3

Explanation: If we flip the bit at the index 3 we will get the distance of 3 from both the 0th and 6th index. For all the other indexes the distance is less than the 3.

Input

int arr[] = {0, 0, 0, 1, 0, 0, 1}

Output

3

Explanation: We can flip the bit at the zeroth index and will get a distance of 3, for the other indexes possible, such as 2 for index 1 and 1 for indexes 2, 4, and 5.

Approach

We have seen the explanations and examples above for the problem, now let us move to the steps required to implement the code:

  • First, we will create a function in which we will pass the array and length of the array as the parameters and it will return an integer.

  • We will create two pointers one will be slow and initially at -1 and always indicated to the last set bit we have found.

  • The fast pointer will traverse over the array and will increase by one every time.

  • At each set bit we will check if the slow pointer is -1 or not because if the slow pointer is -1 indicated we can set the bit of the 0th index otherwise we need to set the bit of the middle index.

  • At each set bit, we will check the middle and update the answer if needed also we will update the position of the slow pointer to the current index.

  • At the end of the loop, we will check the set bit possibility at the last index, and update the value of the answer if it is possible.

Example

#include <bits/stdc++.h>
using namespace std;

// function to find the required maximum distance we will change only 1 bit from it 
int findDis(int arr[], int len){
	int slow = -1, fast = 0;
	int ans = 0; // variable to store the answer 
	// iterating over the array using the pointers and while loop
	while(fast < len ){
	   // if the current index value is 1 
	   if(arr[fast] == 1){
	      // if the slow pointer is at -1 means we hit the first index that contains 1 and now we can flip the value at the zeroth index
	      if(slow == -1 ){
	         // if fast is set bit we cannot flip a zero here 
	         if(fast == 0){
	            slow = fast;
	            fast++; // moving to the next index 
	            continue;
	         }
	         ans = max(ans, fast - slow);
	         slow = fast; 
	      } else{
	         // in this case we will always flip the middle index value 
	         ans = max(ans, (fast-slow+1)/2);
	         slow = fast; // increasing slow pointer to current pointer 
	      }
	   }
	   fast++; // moving to the next index 
	}
	// now check if we can flip the last index 
	ans = max(ans, len -1 -slow); 
	return ans;// returning the final answer 
}

int main(){
   int arr[] = {1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1}; // given array 
	// getting the size of the array 
	int len = sizeof(arr)/ sizeof(arr[0]);
   cout << "The maximum distance at which we can mark the set bit is " << findDis(arr, len) << endl;
	return 0;
}

Output

The maximum distance at which we can mark the set bit is 3

Time and Space Complexity

The time complexity of the above code is O(N), where N is the size of the given array. We are just traversing over the array once makes the time complexity linear.

In this program, we are not using any extra space which makes the time complexity of the above code O(1) or constant.

Note: There will be at least one set bit will be present in the given initial array otherwise getting the distance between any two set bits will not make any sense.

Conclusion

In this tutorial, we have implemented a program to find the maximum distance between two set bits among which one set bit is created by us. We have used the two-pointers approach to traverse over the array with the while loop and stored the answer in a variable. The time complexity of the above code is O(N) as we have traversed over the array only once and no extra space is consumed.

Updated on: 31-Aug-2023

31 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements