---
# System prepended metadata

title: Leetcode 1523. Count Odd Numbers in an Interval Range
tags: [daily, leetcode]

---

# Leetcode 1523. Count Odd Numbers in an Interval Range
###### tags: `leetcode` `daily` 

[題目連結](https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/)

# Method 
:::info
:bulb: **作法講解**:
concept:
if first number is odd, the order is odd, even, odd, even, odd 
=> odd count is first odd + generate one odd in each two character.
=> (low & 1) + (cnt - 1) / 2;
if first number is even, the order is even, odd, even odd
=> even count is generate one odd in each two character.
=> cnt / 2;
:::

TC: O(N) SC: O(N)
:::spoiler 完整程式碼
```cpp=
class Solution {
public:
    int countOdds(int low, int high) {
        int cnt = high - low + 1;
        if(cnt & 0x1) {
            return (low & 0x1) + (cnt-1)/2;
        }
        return cnt / 2;
    }
};
```
:::


