LeetCode Entry
1523. Count Odd Numbers in an Interval Range
fun countOdds(low: Int, high: Int): Int {
1523. Count Odd Numbers in an Interval Range easy
fun countOdds(low: Int, high: Int): Int {
if (low == high) return if (low % 2 == 0) 0 else 1
val lowOdd = low % 2 != 0
val highOdd = high % 2 != 0
val count = high - low + 1
return if (lowOdd && highOdd) {
1 + count / 2
} else if (lowOdd || highOdd) {
1 + (count - 1) / 2
} else {
1 + ((count - 2) / 2)
}
}
Join me on Telegram
https://t.me/leetcode_daily_unstoppable/117
Intuition
Count how many numbers in between, subtract even on the start and the end, then divide by 2.
Complexity
- Time complexity: \(O(1)\)
- Space complexity: \(O(1)\)