0% found this document useful (0 votes)
77 views15 pages

L17 - Longest Sequence of 1 After Flipping A Bit

Uploaded by

Siddharth Jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views15 pages

L17 - Longest Sequence of 1 After Flipping A Bit

Uploaded by

Siddharth Jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

LONGEST SEQUENCE

OF 1’S AFTER FLIP


Introduction
❑ The "Longest Sequence of 1s after flip" problem involves finding the
maximum length of a sequence of consecutive 1s that can be obtained by
flipping a limited number of 0s to 1s.

❑ This problem is commonly encountered in computer science, specifically in the


context of binary strings or arrays.

❑ The goal is to identify the longest sequence of 1s that can be achieved by


flipping a predetermined number of 0s to 1s.
❑ The flip operation involves changing a 0 to a 1. By strategically selecting which
0s to flip, we aim to maximize the length of the resulting sequence of 1s.

❑ The aim is to identify the configuration that yields the maximum length of
consecutive ones within the allowed number of flips.

❑ It's important to note that the number of allowed flips can greatly impact the
result. A higher number of flips generally allows for a longer sequence of ones,
while a limited number of flips may result in a shorter sequence.
Example: 11011101111
Step 1:

11011101111

11111101111
Flipping the First 0 results in 11111101111
length of the sequence of consecutive 1s=6
Step2:

11011101111

11011111111

Flipping the second 0 results in 11011111111


length of the sequence of consecutive 1s=8
where the longest sequence of 1s is 8 long.
Example: 1110001111 k=2
Step 1:

1110001111

1111101111

After Flipping 0 results in 1111101111


length of the sequence of consecutive 1s=5
Step 2:

1110001111

1110111111

After Flipping 0 results in 1110111111


length of the sequence of consecutive 1s=6
where the longest sequence of 1s is 6 long.
public class LongestSequenceOfOnes
{
public static int longestSequenceOfOnes(int binaryNumber) {
String binaryString = Integer.toBinaryString(binaryNumber);
int count = 0;
int maxCount = 0;

for (char digit : binaryString.toCharArray()) {


if (digit == '1’) {
count++;
if (count > maxCount) {
maxCount = count;
}} else{
count = 0;
}
} return maxCount;
}
public static void main(String[] args) {
int binaryNumber = 0b1101110111111001; // Binary representation of 87769
int longestSequence = longestSequenceOfOnes(binaryNumber);
System.out.println("The longest sequence of 1's in " +
Integer.toBinaryString(binaryNumber) + " is: " + longestSequence);
}
}
Time Complexity and space complexity
Time Complexity: O(n), where n is the number of
bits in the binary representation of the given number.

Space Complexity: O(1)


Interview questions

Describe the problem of finding the longest sequence of 1s after


flipping a maximum of 'k' 0s.

This problem involves finding the longest contiguous subarray


consisting of 1s, where we are allowed to flip at most 'k' 0s to 1s.
The goal is to determine the maximum length of such a subarray.
Interview questions

What is the time complexity of your solution?

The time complexity is O(n), where n is the length of the binary


sequence. We iterate through the sequence only once, and each
iteration takes constant time operations.
Interview questions

How would you modify the solution if you were allowed to flip
any number of 0s?

If we can flip any number of 0s, we can simply count the number of
0s encountered and keep track of the maximum length of the
sequence of 1s seen so far. Whenever we encounter a 0, we update
the maximum length if necessary and reset the count of 0s.
Interview questions
What is the space complexity of the given algorithm to find the
longest sequence of 1s after flip?

The space complexity of the given algorithm is O(1), constant space.


It does not use any extra space that grows with the input size. The
algorithm only requires a few variables to keep track of the current
length, maximum length, and the count of 0s encountered, which
remain constant regardless of the input size.
THANK YOU

You might also like