Task Description
- 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.
Code
package solution
func getBinary(N int) int {
return N % 2
}
func Solution(N int) int {
var test = []int{}
var count int = -1
var maxval int = 0
for N>0 {
test = append(test, getBinary(N))
N = N/2
if test[len(test)-1] == 1 {
if maxval < count {
maxval = count
}
count = 0
}else{
if count >= 0 {
count += 1
}
}
}
return maxval
}
causion
- If binary numbers end with 0, then last zero interval doesn't count. but, it's not meaning the answer is 0. The maximum interval value still can be answer.
get 100%

yes!