34
\$\begingroup\$

Goal

Given a non-negative integer, create a function that returns the starting position of number of largest consecutive 1's in that integer's binary value.

When given an input 0, return 0.

If the number has multiple streaks of equal length, you must return the position of the last streak.

Input

An integer greater than or equal to 0.

Output

An integer calculated as explained below.

Rules

  • This is code-golf, so the shortest code in bytes in each language wins.
  • Standard loopholes are forbidden.

Examples and Test Cases

Example 1

  • Your function is passed the integer 142
  • 142 is equal to 10001110 in binary
  • Longest streak is "111" (a streak of three ones)
  • The streak starts at the 2^1 position
  • Your function returns 1 as the result

Example 2

  • Your function is passed the integer 48
  • 48 is equal to 110000 in binary
  • Longest streak is "11" (a streak of two ones)
  • The streak starts at the 2^4 position
  • Your function returns 4 as the result

Example 3

  • Your function is passed the integer 750
  • 750 is equal to 1011101110 in binary
  • Longest streak is "111" (a streak of three ones)
  • Since there are two streaks of equal length, we return the later streak.
  • The later streak starts at the 2^5 position
  • Your function returns 5 as the result
\$\endgroup\$
8
  • 1
    \$\begingroup\$ You need a winning criterion, like code-golf \$\endgroup\$
    – Okx
    Commented Sep 16, 2017 at 19:51
  • \$\begingroup\$ @Okx It had been mentioned in the body itself so I added the tag. \$\endgroup\$ Commented Sep 16, 2017 at 20:04
  • \$\begingroup\$ Make sure people test 0. That's an important test case. \$\endgroup\$
    – mbomb007
    Commented Sep 16, 2017 at 23:39
  • 2
    \$\begingroup\$ Instead of "last streak" or "latest streak", I'd suggest "streak with the largest place value". \$\endgroup\$
    – aschepler
    Commented Sep 16, 2017 at 23:50
  • \$\begingroup\$ @Okx Why is a winning criterion necessary? Why can't it simply be a puzzle? \$\endgroup\$
    – corsiKa
    Commented Sep 18, 2017 at 18:46

36 Answers 36

1
2
0
\$\begingroup\$

Objective-C, 96 chars

for(int h=0,c=0,l=0,p=0;;x/=2,p++){if(x%2==0){if(c<=h){h=c;l=p;}c=0;if(x==0){return p;}}else{c++;}}
  • If x is even the highest count is checked and count resets
  • If x is also 0 then return the highest count
  • If x isn't even, then the last digit is 1 and count increments
  • x is divided by two
\$\endgroup\$
2
  • \$\begingroup\$ Isn't this supposed to return the bit position of the highest count, not the count value. \$\endgroup\$
    – cleblanc
    Commented Sep 18, 2017 at 20:07
  • \$\begingroup\$ Yes it is. I did not catch that. Thank you for pointing that out. \$\endgroup\$ Commented Sep 18, 2017 at 23:12
0
\$\begingroup\$

Java 8, 81 bytes

int c(int n){int x=(int)(Math.log(n)/Math.log(2));return(n&=n/2)>0?c(n):x<0?0:x;}

Port of @Arnauld's JavaScript answer.

Try it here.

\$\endgroup\$
0
\$\begingroup\$

Javascript, 71 bytes

I was thinking about regular expressions and...

x=>+x.toString(2).replace(/.*?0?(1*)(?!.*\1[1])(.*)/,(x,m,a)=>a.length)

Regex101 link

\$\endgroup\$
0
\$\begingroup\$

Python 3, 83 bytes

len(bin(num)[2:].split('1'*(max(list(map(len,bin(num)[2:].split('0'))))))[1])

Try it online!

It does not support the condition that two or more arrays of 1s are longest.

\$\endgroup\$
0
\$\begingroup\$

Mathematica, 44 bytes

I feel like I can trim off a few more bytes, though I'm not sure how yet.

We save space by using an assignment within If, using #0 for pure function recursion, and using infix notation when possible.

If[(k=BitAnd[#,#/2])>0,#0@k,Log2@#~BitOr~0]&

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Zsh, 53 bytes

b=$[[##2]$1];set ${(Ons:0:)b};<<<$[$#b-${(SE)b#$1}+1]

Try it online! 58b 60b

b is argument $1 in binary. Then we set $1 again, to the longest sequence of 1s in b.
${(SE)b#$1 finds the first occurrence (from the left) of $1 in b, and returns the index+1 of the last matching char. However since the question is counting by powers of 2 we do a subtraction from the length of b.

Other approaches reverse b, but that took more bytes.

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.