Knowee
Questions
Features
Study Tools

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].

Question

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].

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a Java solution for the problem:

class Solution {
    public int solution(int N) {
        String binaryString = Integer.toBinaryString(N);
        int maxGap = 0;
        int currentGap = 0;
        for (char c : binaryString.toCharArray()) {
            if (c == '0') {
                currentGap++;
            } else {
                if (currentGap > maxGap) {
                    maxGap = currentGap;
                }
                currentGap = 0;
            }
        }
        return maxGap;
    }
}

This function works as follows:

  1. It converts the integer N to a binary string.
  2. It initializes two variables, maxGap and currentGap, to keep track of the longest binary gap and the current binary gap, respectively.
  3. It iterates over each character in the binary string. If the character is '0', it increments currentGap. If the character is '1', it checks if currentGap is greater than maxGap, and if so, it updates maxGap with the value of currentGap. It then resets currentGap to 0.
  4. After the loop, it returns maxGap, which is the length of the longest binary gap.

This problem has been solved

Similar Questions

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.

Given a binary representation in the form of a string(s) of a number n, the task is to find a binary representation of n+1.Note: Output binary string should not contain leading zeros.Example 1:Input: s = "10"Output: 11Explanation: "10" is the binary representation of 2 and binary representation of 3 is "11"Example 2:Input: s = "111"Output: 1000Explanation: "111" is the binary representation of 7 and binary representation of 8 is "1000"Your Task:  You don't need to read input or print anything. Complete the function binaryNextNumber()which takes s as input parameter and returns the string.Expected Time Complexity: O(n)Expected Auxiliary Space: O(n) to store resultant string  Constraints:1 <= n <= 105

Write a function that takes an integer and returns the number of 1 bits it has.Problem Constraints0 <= A <= 4294967295Input FormatFirst and only argument contains integer AOutput FormatReturn an integer as the answerExample InputInput1: 11Example OutputOutput1:3Example ExplanationExplaination1:11 is represented as 1101 in binary

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)

What is binary?*1 pointA sequence of 1s and 0sThe way a computer speaksA useless code that is no longer used

1/1

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.