{"id":7130,"date":"2020-07-25T10:27:02","date_gmt":"2020-07-25T17:27:02","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7130"},"modified":"2020-07-25T10:27:27","modified_gmt":"2020-07-25T17:27:27","slug":"leetcode-1523-count-odd-numbers-in-an-interval-range","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1523-count-odd-numbers-in-an-interval-range\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1523. Count Odd Numbers in an Interval Range"},"content":{"rendered":"\n<p>Given two non-negative integers&nbsp;<code>low<\/code>&nbsp;and&nbsp;<code>high<\/code>. Return the&nbsp;<em>count of odd numbers between&nbsp;<\/em><code>low<\/code><em>&nbsp;and&nbsp;<\/em><code>high<\/code><em>&nbsp;(inclusive)<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> low = 3, high = 7\n<strong>Output:<\/strong> 3\n<strong>Explanation: <\/strong>The odd numbers between 3 and 7 are [3,5,7].<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> low = 8, high = 10\n<strong>Output:<\/strong> 1\n<strong>Explanation: <\/strong>The odd numbers between 8 and 10 are [9].<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>0 &lt;= low &lt;= high&nbsp;&lt;= 10^9<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Math<\/strong><\/h2>\n\n\n\n<p>The count of odd numbers between [1, low &#8211; 1] is low \/ 2<br>e.g. low = 6, we have [1,3,5] in range [1, 5] and count is 6\/2 = 3.<br>The count of odd numbers between [1, high] is (high + 1) \/ 2<br>e.g. high = 7, we have [1,3,5,7] in range [1, 7] and count is (7+1) \/ 2 = 4<\/p>\n\n\n\n<p>Then the count of odd numbers in range [low, high] = count(1, high) &#8211; count(1, low-1)<br>e.g. in range [6, 7] we only have [7], count: 4 &#8211; 3 = 1<\/p>\n\n\n\n<p>ans = (high + 1) \/ 2 &#8211; low \/ 2<\/p>\n\n\n\n<p>Time complexity: O(1)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  int countOdds(int low, int high) {\n    return (high + 1) \/ 2 - low \/ 2;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given two non-negative integers&nbsp;low&nbsp;and&nbsp;high. Return the&nbsp;count of odd numbers between&nbsp;low&nbsp;and&nbsp;high&nbsp;(inclusive). Example 1: Input: low = 3, high = 7 Output: 3 Explanation: The odd numbers&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[222,409,31,408],"class_list":["post-7130","post","type-post","status-publish","format-standard","hentry","category-math","tag-easy","tag-even","tag-math","tag-odd","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7130"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7130\/revisions"}],"predecessor-version":[{"id":7132,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7130\/revisions\/7132"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}