Find Index of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array - GeeksforGeeks
Given an array of 0s and 1s, find the position of 0 to be replaced with 1 to get longest continuous sequence of 1s. Expected time complexity is O(n) and auxiliary space is O(1).
The idea is to keep track of three indexes, current index (curr), previous zero index (prev_zero) and previous to previous zero index (prev_prev_zero). Traverse the array, if current element is 0, calculate the difference between curr and prev_prev_zero (This difference minus one is the number of 1s around the prev_zero). If the difference between curr and prev_prev_zero is more than maximum so far, then update the maximum. Finally return index of the prev_zero with maximum difference.
A simple solution is to store binary representation of given number in a binary array
Read full article from Find Index of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array - GeeksforGeeks
Given an array of 0s and 1s, find the position of 0 to be replaced with 1 to get longest continuous sequence of 1s. Expected time complexity is O(n) and auxiliary space is O(1).
The idea is to keep track of three indexes, current index (curr), previous zero index (prev_zero) and previous to previous zero index (prev_prev_zero). Traverse the array, if current element is 0, calculate the difference between curr and prev_prev_zero (This difference minus one is the number of 1s around the prev_zero). If the difference between curr and prev_prev_zero is more than maximum so far, then update the maximum. Finally return index of the prev_zero with maximum difference.
static
int
maxOnesIndex(
int
arr[],
int
n)
{
int
max_count =
0
;
// for maximum number of 1 around a zero
int
max_index=
0
;
// for storing result
int
prev_zero = -
1
;
// index of previous zero
int
prev_prev_zero = -
1
;
// index of previous to previous zero
// Traverse the input array
for
(
int
curr=
0
; curr<n; ++curr)
{
// If current element is 0, then calculate the difference
// between curr and prev_prev_zero
if
(arr[curr] ==
0
)
{
// Update result if count of 1s around prev_zero is more
if
(curr - prev_prev_zero > max_count)
{
max_count = curr - prev_prev_zero;
max_index = prev_zero;
}
// Update for next iteration
prev_prev_zero = prev_zero;
prev_zero = curr;
}
}
// Check for the last encountered zero
if
(n-prev_prev_zero > max_count)
max_index = prev_zero;
return
max_index;
}
A Simple Solution is to traverse the array, for every 0, count the number of 1s on both sides of it. Keep track of maximum count for any 0. Finally return index of the 0 with maximum number of 1s around it. The time complexity of this solution is O(n2).
http://www.geeksforgeeks.org/find-longest-sequence-1s-binary-representation-one-flip/
Give an integer n. We can flip exactly one bit. Write code to find the length of the longest sequence of 1 s you could create.
An efficient solution is to walk through the bits in binary representation of given number. We keep track of current 1’s sequence length and the previous l’s sequence length. When we see a zero, update previous Length:
- If the next bit is a 1, previous Length should be set to current Length.
- If the next bit is a 0, then we can’t merge these sequences together. So, set previous Length to 0.
We update max length (or result) by comparing following two:
- Current value of max length
- Current-Length + Previous-Length + 1.
int
flipBit(unsigned a)
{
/* If all bits are l, binary
representation of 'a' has all 1s */
if
(~a == 0)
return
8*
sizeof
(
int
);
int
currLen = 0, prevLen = 0;
// We can always have a sequence of
// at least one 1
int
maxLen = 1;
while
(a!= 0)
{
// Current bit is a 1
if
((a & 1) == 1)
currLen++;
// Current bit is a 0
else
if
((a & 1) == 0)
{
/* Update to 0 (if next bit is 0)
or currLen (if next bit is 1). */
prevLen = (a & 2) == 0? 0 : currLen;
currLen = 0;
}
// Update maxLen if required
maxLen = max(prevLen + currLen + 1, maxLen);
// Remove last bit (Right shift)
a >>= 1;
}
return
maxLen;
}
Read full article from Find Index of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array - GeeksforGeeks