object LongestZeroes {
// Function to find the longest sequence of consecutive zeroes in binary representation
def longestConsecutiveZeroes(n: Int): Int = {
var maxCount = 0
var currentCount = 0
var num = n
while (num > 0) {
if ((num & 1) == 0) { // Check if the least significant bit is 0
currentCount += 1
maxCount = math.max(maxCount, currentCount) // Update maxCount
} else {
currentCount = 0 // Reset count when a 1 is encountered
}
num >>= 1 // Right shift the number
}
maxCount
}
def main(args: Array[String]): Unit = {
val num = 11298 // Binary: 0010110000100010
println(s"Longest consecutive zeroes: ${longestConsecutiveZeroes(num)}")
}
}
/*
run:
Longest consecutive zeroes: 4
*/