diff options
author | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
---|---|---|
committer | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
commit | d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch) | |
tree | 96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /count_odd_num_interval_range.c | |
download | leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip |
initial
Diffstat (limited to 'count_odd_num_interval_range.c')
-rw-r--r-- | count_odd_num_interval_range.c | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/count_odd_num_interval_range.c b/count_odd_num_interval_range.c new file mode 100644 index 0000000..e8a80f3 --- /dev/null +++ b/count_odd_num_interval_range.c @@ -0,0 +1,21 @@ +// 1523. Count Odd Numbers in an Interval Range +#include <math.h> +#include <stdio.h> + +// given two non-negative integers 'low' and 'high' return the +// count of odd numbers * between 'low' and 'high' (incusively) + +int countOdds(int low, int high) { + int ans = 0; + if (low % 2) { + ans += (high - low) / 2; + ans++; + } else + ans += (double)ceil((high - low) / 2.0); + return ans; +} + +int main() { + printf("%d\n", countOdds(3, 7)); // expect: 3 + printf("%d\n", countOdds(8, 10)); // expect: 1 +} |