# Binary Gap

jo1132·2022년 5월 19일
0

## GO Language

목록 보기
1/1

• 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 { // get binary num to divede by 2
return N % 2
}

func Solution(N int) int {
var test = []int{}		// save binary to int slice
var count int = -1		// save the zero interval, when test slice start 0, count doesn't add 1.
var maxval int = 0		// the max internal value

for N>0 {
test = append(test, getBinary(N))	// test get binary nums, but it's reverse
N = N/2

if test[len(test)-1] == 1 {			// if binary num is 1, then check maxvalue and set 0 to count
if maxval < count {
maxval = count
}
count = 0
}else{								// when binary num is 0, count add 1. but, if count <-1 (slice started with 0) then count don't precess
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.

yes!

Talking Potato