Count Odd Numbers in an Interval Range

Given two non-negative integers low and high, return how many odd numbers lie between low and high, both included.

30:00

Count Odd Numbers in an Interval Range
easy
Free written solution
Topics
Companies

Given two non-negative integers low and high, return how many odd numbers lie between low and high, both included.

Example 1:
Input: {"low":3,"high":7}
Output: 3
Constraints:
  • 0≤low≤high≤1090 \leq \text{low} \leq \text{high} \leq 10^9

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"low":3,"high":7}

Count the odd numbers in [3, 7]. Instead of checking 5 numbers, count odds from 0 up to high, then remove the ones below low.

0..7 in pairs (even, odd)

0
1
2
3
4
5
6
7

■ odd, counted

prefix counts

odds in [0, 7] = (high + 1) // 2

odds in [0, 2] = low // 2

odds in [3, 7]

answer

? − ? = ?
Variables
VariableValue
low3
high7
DepthFunction Call
Stack empty
0/7