commit 88ae2cc953fbe1dfa4a77b361605103efa8597b6
parent 8875111b46c3d020eb593ec8cb85bc22d4fc5a34
Author: Jack Sangdahl <jack@pngu.org>
Date: Sun, 7 Dec 2025 08:49:28 +0100
1523. count odd numbers in an interval range
Diffstat:
2 files changed, 24 insertions(+), 11 deletions(-)
diff --git a/count_odd_num_interval_range.c b/count_odd_num_interval_range.c
@@ -1,19 +1,10 @@
// 1523. Count Odd Numbers in an Interval Range
-#include <math.h>
-#include <stdio.h>
+#include "leetcode.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 countOdds(int low, int high) { return (high - (low & ~1) + 1) >> 1; }
int main() {
printf("%d\n", countOdds(3, 7)); // expect: 3
diff --git a/count_odd_num_interval_range.py b/count_odd_num_interval_range.py
@@ -0,0 +1,22 @@
+# 1523. Count Odd Numbers in an Interval Range
+
+"""
+given two non-negative integers 'low' and 'high' return the
+count of odd numbers * between 'low' and 'high' (incusively)
+"""
+
+
+class Solution(object):
+ def countOdds(self, low, high):
+ """
+ :type low: int
+ :type high: int
+ :rtype: int
+ """
+ return (high + 1) // 2 - low // 2
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.countOdds(low=3, high=7))
+ print(obj.countOdds(low=8, high=10))