-2

A non-negative integer N is given. The maximal binary ones stretch of N is the length of the longest sequence of consecutive bits set to 1 in the binary representation of N. For example, consider N = 114, 067. Its binary representation is 11011110110010011. Its maximal binary stretch of ones is equal to 4 from the above description.

Can someone tell me how to approach the above problem in C programming.

Johnny P
  • 9
  • 3
  • Mask and shift. Do you have a more specific question? – Carl Norum Aug 26 '14 at 18:04
  • The problem for me here is how I can keep the track of ones. Here the occurrences of ones in a stretch is 11, 1111, 11, 1, 11. But I have to return 4 since the maximum occurrences of ones in a stretch is 4. If Im traversing the bits from left to right or right to left, how I can keep track of them and then return the highest consecutive occurrences of ones? – Johnny P Aug 26 '14 at 18:08
  • 2
    Keep a count of the maximum value, (Initialize it to 0). – Martin James Aug 26 '14 at 18:13
  • can you provide me with a code snippet. Im new to programming. – Johnny P Aug 26 '14 at 18:36
  • 1
    This is worded as either homework or a programming problem. Either way, getting other people to tell you how to approach it defeats the purpose. – Dan Fego Aug 26 '14 at 18:49
  • 2
    This question appears to be off-topic because it fails to show any effort. – Code-Apprentice Aug 26 '14 at 18:52
  • I was not able to keep the track of ones in the binary format. Hence I posted the question. Im new to the stackoverflow too. Next I shall post my code when I request for the improvisation. Point taken. PS: In the first place, I didnt post my code snippet probably because it is not up to the standards. Im improving. How about the experts guide me where I can start out on gaining expertise on C programming? – Johnny P Aug 26 '14 at 19:17

1 Answers1

1

Damn-short solution, I could fit it in a comment:

#include<stdio.h>
main(N,L,B)
{
    N=114067,L=B=N&1;
    while(N>>=1)B=(L=(N&1)?L+1:0)>B?L:B;
    return !printf("%d",B);
}

Live demo link.


Nah... that was too short to understand anything, let's expand it a little:

#include <stdio.h>

int main()
{
    int N = 0; // input value, will be read with scanf()
    int currentLength = 0; // here we will store the current number of consecutive ones
    int bestLength = 0; // here we will store the best result

    scanf("%d", &N); // read the input value from standard input

    while (N) // as long as N is greater than 0
    {
        if (N & 1) // if the last bit is set to 1
        {
            // cool, let's increment the current sequence's length
            currentLength += 1;

            // currentLength has changed, maybe now it is better than best known solution?
            if (currentLength > bestLength)
            {
                // awesome! new best solution is found
                bestLength = currentLength;
            }
        }
        else
        {
            // we have encountered 0 while scanning bits, we must start counting the length over
            currentLength = 0;
        }

        // let's move to the next bit!
        N = N >> 1;
    }

    printf("%d", bestLength); // print out the value

    return 0;
}

One more live demo link.

Piotr Skotnicki
  • 46,953
  • 7
  • 118
  • 160