Problem DescriptionYou are given a binary string B of length L which contains K ones and remaining zeros. You are required to place the K ones in the binary string in such a way that the longest consecutive zeros have the least length possible. Once such a binary string is constructed, you are required to print the length of the contiguous block of zeros, which has the largest length.Constraints0 <= K <= L1 <= L <= 10^6InputSingle line consisting of two space separated integers denoting L and K.OutputPrint a single integer denoting the length of the longest consecutive zeros as per the problem.Time Limit (secs)1ExamplesExample 1Input3 1Output1ExplanationB is of length 3 and it has 1 one's.So the possible strings as per the problem are 010, 001, 100.In the first case, the maximum length of consecutive zeros is 1 whereas in the other two cases it is 2. Hence the constructed binary string is 010 and the output is 1.Example 2Input3 3Output0ExplanationB is of length 3 and it has all three one's. There is no block of zeros, hence the output is 0.
Question
Problem DescriptionYou are given a binary string B of length L which contains K ones and remaining zeros. You are required to place the K ones in the binary string in such a way that the longest consecutive zeros have the least length possible. Once such a binary string is constructed, you are required to print the length of the contiguous block of zeros, which has the largest length.Constraints0 <= K <= L1 <= L <= 10^6InputSingle line consisting of two space separated integers denoting L and K.OutputPrint a single integer denoting the length of the longest consecutive zeros as per the problem.Time Limit (secs)1ExamplesExample 1Input3 1Output1ExplanationB is of length 3 and it has 1 one's.So the possible strings as per the problem are 010, 001, 100.In the first case, the maximum length of consecutive zeros is 1 whereas in the other two cases it is 2. Hence the constructed binary string is 010 and the output is 1.Example 2Input3 3Output0ExplanationB is of length 3 and it has all three one's. There is no block of zeros, hence the output is 0.
Solution
The problem is asking to find the maximum length of consecutive zeros in a binary string of length L which contains K ones. The ones should be placed in such a way that the longest consecutive zeros have the least length possible.
Here are the steps to solve the problem:
-
First, we need to understand that to minimize the length of the longest consecutive zeros, we should distribute the ones as evenly as possible in the binary string.
-
If K is 0, it means there are no ones, so the length of the longest consecutive zeros is L.
-
If K is not 0, we calculate the length of zeros between ones. This can be done by dividing L by K. Let's denote this value as 'd'. This is because we are trying to distribute K ones in L length, so on average, there will be L/K length of zeros between ones.
-
However, there might be some remaining zeros after distributing ones. This can be calculated by taking the modulus of L and K. Let's denote this value as 'r'.
-
The length of the longest consecutive zeros is 'd'. But if there are remaining zeros (r > 0), it means there are some gaps of zeros that have length 'd+1'. The number of such gaps is 'r'.
-
So, the final answer is 'd' if r is 0, otherwise 'd+1'.
Here is a Python code for the problem:
def max_zeros(L, K):
if K == 0:
return L
d, r = divmod(L, K)
return d if r == 0 else d + 1
This function takes the length of the binary string and the number of ones as input, and returns the length of the longest consecutive zeros.
Similar Questions
Given an integer A, count and return the number of trailing zeroes.Problem Constraints1 <= A <= 109Input FormatFirst and only argument is an integer A.Output FormatReturn an integer denoting the count of trailing zeroes.Example InputInput 1: A = 18Input 2: A = 8Example OutputOutput 1: 1Output 2: 3Example ExplanationExplanation 1: 18 in binary is represented as: 10010, there is 1 trailing zero.Explanation 2: 8 in binary is represented as: 1000, there are 3 trailing zeroes.
In this problem, you have to add and multiply huge numbers! These numbers are so big that you can't contain them in any ordinary data types like a long integer.Use the power of Java's BigInteger class and solve this problem.Input FormatThere will be two lines containing two numbers, and .Constraints and are non-negative integers and can have maximum digits.Output FormatOutput two lines. The first line should contain , and the second line should contain . Don't print any leading zeros.Sample Input123420Sample Output125424680
A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has binary representation 10100 and contains one binary gap of length 1. The number 15 has binary representation 1111 and has no binary gaps. The number 32 has binary representation 100000 and has no binary gaps.Write a function:class Solution { public int solution(int N); }that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn't contain a binary gap.For example, given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its longest binary gap is of length 5. Given N = 32 the function should return 0, because N has binary representation '100000' and thus no binary gaps.Write an efficient algorithm for the following assumptions:N is an integer within the range [1..2,147,483,647].
Given a number, swap the adjacent bits in the binary representation of the number, and print the new number formed after swapping.Input FormatThe first line of input contains T - the number of test cases. Each of the next T lines contains a number N.Output FormatFor each test case, print the new integer formed after swapping adjacent bits, separated by a new line.Constraints1 <= T <= 1000000 <= N <= 109ExampleInput410743100Output51123152ExplanationTest-Case 1Binary Representation of 10: 000...1010After swapping adjacent bits: 000...0101 (5)Test-Case 2Binary Representation of 7: 000...0111After swapping adjacent bits: 000...1011 (11)
Problem DescriptionYou have 2 strings (A and B).You have to concatenate two strings and return the concatenated string.Problem Constraints1 <= A.size() <= 10001 <= B.size() <= 1000
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.