{"id":360,"date":"2025-06-24T11:59:11","date_gmt":"2025-06-24T06:29:11","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=360"},"modified":"2025-07-07T20:50:20","modified_gmt":"2025-07-07T15:20:20","slug":"find-the-missing-number","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/find-the-missing-number\/","title":{"rendered":"Missing Number | Leetcode 268 | Explained"},"content":{"rendered":"\n<p>Given an array&nbsp;<code>nums<\/code>&nbsp;containing&nbsp;<code>n<\/code>&nbsp;distinct numbers in the range&nbsp;<code>[0, n]<\/code>, return&nbsp;<em>the only number in the range that is missing from the array.<\/em><\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/leetcode.com\/problems\/missing-number\/description\/\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Example 1:<\/strong><br><strong>Input:<\/strong> nums = [3,0,1]<br><strong>Output:<\/strong> 2<br><strong>Explanation:<\/strong> n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums.<\/p>\n\n\n\n<p><strong>Example 2:<\/strong><br><strong>Input:<\/strong> nums = [0,1]<br><strong>Output:<\/strong> 2<br><strong>Explanation:<\/strong> n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums.<\/p>\n\n\n\n<p><strong>Example 3:<\/strong><br><strong>Input: <\/strong>nums = [9,6,4,2,3,5,7,0,1]<br><strong>Output:<\/strong> 8<br><strong>Explanation:<\/strong> n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums.<\/p>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>n == nums.length<\/li>\n\n\n\n<li>1 &lt;= n &lt;= 10<sup>4<\/sup><\/li>\n\n\n\n<li>0 &lt;= nums[i] &lt;= n<\/li>\n\n\n\n<li>All the numbers of <strong>nums <\/strong>are <strong>unique<\/strong>.<\/li>\n<\/ul>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents ub_table-of-contents-collapsed\" id=\"ub_table-of-contents-eb8a1835-b4c8-4575-b1d8-a853ebaeb72b\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"true\" data-initiallyshow=\"false\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Contents:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">show<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column ub-hide\">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-missing-number\/#0-brute-force-solution\" style=\"\">BRUTE FORCE SOLUTION<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-missing-number\/#1-1-what-does-the-question-says-\" style=\"\">1. What does the question says?<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-missing-number\/#2-2-how-do-we-approach-the-problem-\" style=\"\">2. How do we approach the problem?<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-missing-number\/#3-3-code-implementation-\" style=\"\">3. Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-missing-number\/#4-4-step-by-step-explanation-\" style=\"\">4. Step-by-Step Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-missing-number\/#5-5-dry-run-\" style=\"\">5. Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-missing-number\/#6-6-potential-edge-cases-\" style=\"\">6. Potential Edge Cases<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-missing-number\/#7-7-time-and-space-complexity-\" style=\"\">7. Time and Space Complexity<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-missing-number\/#8-better-solution\" style=\"\">BETTER SOLUTION<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-missing-number\/#9-1-what-does-the-question-says-\" style=\"\">1. What does the question says?<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-missing-number\/#10-2-how-do-we-approach-the-problem-\" style=\"\">2. How do we approach the problem?<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-missing-number\/#11-3-code-implementation-\" style=\"\">3. Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-missing-number\/#12-4-step-by-step-explanation-\" style=\"\">4. Step-by-Step Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-missing-number\/#13-5-dry-run-\" style=\"\">5. Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-missing-number\/#14-6-potential-edge-cases-\" style=\"\">6. Potential Edge Cases<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-missing-number\/#15-7-time-and-space-complexity-\" style=\"\">7. Time and Space Complexity<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-missing-number\/#16-optimal-solution\" style=\"\">OPTIMAL SOLUTION<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-missing-number\/#17-1-what-does-the-question-say-\" style=\"\">1. What does the question say?<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-missing-number\/#18-2-how-do-we-approach-the-problem-\" style=\"\">2. How do we approach the problem?<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-missing-number\/#19-3-implementation-\" style=\"\">3. Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-missing-number\/#20-4-step-by-step-explanation-\" style=\"\">4. Step-by-Step Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-missing-number\/#21-5-dry-run-\" style=\"\">5. Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-missing-number\/#22-6-potential-edge-cases-\" style=\"\">6. Potential Edge Cases<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-the-missing-number\/#23-7-time-and-space-complexity-\" style=\"\">7. Time and Space Complexity<\/a><\/li><\/ul><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"0-brute-force-solution\">BRUTE FORCE SOLUTION<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-1-what-does-the-question-says-\"><strong>1. What does the question says?<\/strong><\/h3>\n\n\n\n<p>We have an array of integers named nums containing <strong>n<\/strong> distinct numbers taken from the set <strong>{0, 1, 2, \u2026, n}<\/strong>. Out of these <strong>n + 1<\/strong> possible numbers, exactly one is missing in the array. We need to identify which number from <strong>0 to n<\/strong> is not present in nums and return it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-2-how-do-we-approach-the-problem-\"><strong>2. How do we approach the problem?<\/strong><\/h3>\n\n\n\n<p>The provided approach uses a simple membership check for each candidate number:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>We know that the missing number has to be some integer from <strong>0 to n<\/strong>.<\/li>\n\n\n\n<li>We check sequentially from<strong> i = 0<\/strong> up to <strong>i = n<\/strong> whether<strong> i <\/strong>is not in the list nums.<\/li>\n\n\n\n<li>Once we find such an <strong>i<\/strong>, we return it because it must be the missing number.<\/li>\n<\/ol>\n\n\n\n<p>This method guarantees correctness because:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>By iterating from <strong>0 to n<\/strong>, we cover all possible numbers in the required range.<\/li>\n\n\n\n<li>The first number that is not found in nums is, by definition, the missing one.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-3-code-implementation-\"><strong>3. Code Implementation<\/strong><\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def missingNumber(self, nums: List[int]) -&gt; int:\n        for i in range(0, len(nums) + 1):\n            if i not in nums:\n                return i\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">missingNumber<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">nums<\/span><span style=\"color: #D4D4D4\">: List[<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">]) -&gt; <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> i <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(nums) + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> i <\/span><span style=\"color: #569CD6\">not<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">in<\/span><span style=\"color: #D4D4D4\"> nums:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> i<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-4-step-by-step-explanation-\"><strong>4. Step-by-Step Explanation<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Loop Through Possible Numbers:<\/strong> Go from <strong>i = 0 <\/strong>up to <strong>i = len(nums)<\/strong> (inclusive).<\/li>\n\n\n\n<li><strong>Check Membership:<\/strong> For each i, we check if i is not present in the list nums.<\/li>\n\n\n\n<li><strong>Return If Absent:<\/strong> The moment we find <strong>i<\/strong> that doesn\u2019t exist in nums, we return i as the missing number.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-5-dry-run-\"><strong>5. Dry Run<\/strong><\/h3>\n\n\n\n<p>Let&#8217;s go through the code step by step, using images to illustrate the detailed dry run process.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-2-1024x576.png\" alt=\"\" class=\"wp-image-362\" srcset=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-2-1024x576.png 1024w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-2-300x169.png 300w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-2-768x432.png 768w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-2-1536x864.png 1536w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-2-150x84.png 150w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-2-450x253.png 450w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-2-1200x675.png 1200w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-2.png 1600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-6-potential-edge-cases-\"><strong>6. Potential Edge Cases<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Smallest Array:<\/strong>\n<ul class=\"wp-block-list\">\n<li>If nums = [1] (n = 1), the missing number might be 0.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>All Elements Present Except the Last:<\/strong>\n<ul class=\"wp-block-list\">\n<li>If nums = [0, 1, 2, \u2026, n-1], then the missing number is n.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Missing 0:<\/strong>\n<ul class=\"wp-block-list\">\n<li>If nums = [1, 2, 3, \u2026, n], return 0.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Already In Order:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Even if nums is sorted [0, 1, 2, 3, \u2026, m-1, m+1, \u2026, n], our logic still checks each number.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-7-time-and-space-complexity-\"><strong>7. Time and Space Complexity<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong><strong>O(n<\/strong><strong><sup>2<\/sup><\/strong><strong>)<\/strong> in the worst case because:\n<ul class=\"wp-block-list\">\n<li>We are iterating from <strong>0 to n <\/strong>(which is <strong>n+1 <\/strong>iterations).<\/li>\n\n\n\n<li>Each membership check <strong>(i not in nums)<\/strong> is <strong>O(n)<\/strong> for a list.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Space Complexity: O(1)<\/strong>, as we only use extra variables for iterating (i) and do not allocate significant additional space.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"8-better-solution\">BETTER SOLUTION<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-1-what-does-the-question-says-\"><strong>1. What does the question says?<\/strong><\/h3>\n\n\n\n<p>We have an array of integers named nums containing <strong>n<\/strong> distinct numbers taken from the set <strong>{0, 1, 2, \u2026, n}<\/strong>. Out of these <strong>n + 1<\/strong> possible numbers, exactly one is missing in the array. Our goal is to identify which number from 0 to n is missing and return it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"10-2-how-do-we-approach-the-problem-\"><strong>2. How do we approach the problem?<\/strong><\/h3>\n\n\n\n<p>This approach uses a <strong>frequency map<\/strong> (dictionary) to keep track of which numbers appear in the array:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Create a dictionary freq where each key in the range <strong>[0 \u2026 n]<\/strong> is initialized to <strong>0<\/strong>.<\/li>\n\n\n\n<li>Iterate over the elements of nums and increment the corresponding value in freq.<\/li>\n\n\n\n<li>Finally, check which key in freq remains at 0, meaning that number never appeared in nums. Return this key.<\/li>\n<\/ol>\n\n\n\n<p>By doing so, we ensure every number that appears in nums is marked. The one that remains at a <strong>count of 0<\/strong> is the missing number.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11-3-code-implementation-\"><strong>3. Code Implementation<\/strong><\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def missingNumber(self, nums: List[int]) -&gt; int:\n        freq = {i: 0 for i in range(0, len(nums) + 1)}\n        for i in nums:\n            freq[i] += 1\n        for k, v in freq.items():\n            if v == 0:\n                return k\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">missingNumber<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">nums<\/span><span style=\"color: #D4D4D4\">: List[<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">]) -&gt; <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        freq = {i: <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> i <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(nums) + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">)}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> i <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> nums:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            freq[i] += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> k, v <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> freq.items():<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> v == <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> k<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"12-4-step-by-step-explanation-\"><strong>4. Step-by-Step Explanation<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Initialize a dictionary freq with keys from 0 up to len(nums) (inclusive), each set to 0.<\/li>\n\n\n\n<li>Traverse the list nums and increment freq[i] for each number i in nums.<\/li>\n\n\n\n<li>Iterate over each key-value pair in freq. The first key (k) whose value (v) is <strong>0<\/strong> is the missing number, so return<strong> k.<\/strong><\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"13-5-dry-run-\"><strong>5. Dry Run<\/strong><\/h3>\n\n\n\n<p>Let&#8217;s go through the code step by step, using images to illustrate the detailed dry run process.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-3-1024x576.png\" alt=\"\" class=\"wp-image-363\" srcset=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-3-1024x576.png 1024w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-3-300x169.png 300w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-3-768x432.png 768w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-3-1536x864.png 1536w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-3-150x84.png 150w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-3-450x253.png 450w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-3-1200x675.png 1200w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-3.png 1600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-4-1024x576.png\" alt=\"\" class=\"wp-image-364\" srcset=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-4-1024x576.png 1024w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-4-300x169.png 300w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-4-768x432.png 768w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-4-1536x864.png 1536w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-4-150x84.png 150w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-4-450x253.png 450w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-4-1200x675.png 1200w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-4.png 1600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-5-1024x576.png\" alt=\"\" class=\"wp-image-365\" srcset=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-5-1024x576.png 1024w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-5-300x169.png 300w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-5-768x432.png 768w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-5-1536x864.png 1536w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-5-150x84.png 150w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-5-450x253.png 450w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-5-1200x675.png 1200w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/image-5.png 1600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"14-6-potential-edge-cases-\"><strong>6. Potential Edge Cases<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Smallest Array:\n<ul class=\"wp-block-list\">\n<li>If nums = [1] (which means n = 1), the missing number is 0.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>All Elements Present Except the Last:\n<ul class=\"wp-block-list\">\n<li>If nums = [0, 1, 2, \u2026, n-1], the missing number is n.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Missing 0:\n<ul class=\"wp-block-list\">\n<li>If nums = [1, 2, 3, \u2026, n], return 0.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Already In Order:\n<ul class=\"wp-block-list\">\n<li>If nums is sorted [0, 1, 2, \u2026, m-1, m+1, \u2026, n], the dictionary will still capture the missing number.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"15-7-time-and-space-complexity-\"><strong>7. Time and Space Complexity<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity<\/strong>: <strong>O(n)<\/strong> to populate freq plus another <strong>O(n) <\/strong>to iterate over its items, which simplifies to <strong>O(2n) = O(n)<\/strong>.<\/li>\n\n\n\n<li><strong>Space Complexity<\/strong>: <strong>O(n) <\/strong>because we create a dictionary of size <strong>n+1<\/strong>.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"16-optimal-solution\">OPTIMAL SOLUTION<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"17-1-what-does-the-question-say-\"><strong>1. What does the question say?<\/strong><\/h3>\n\n\n\n<p>We have an array of integers named nums containing <strong>n<\/strong> distinct numbers taken from the set <strong>{0, 1, 2, \u2026, n}<\/strong>. Out of these <strong>n + 1<\/strong> possible numbers, exactly one is missing in the array. We need to figure out which number in the range 0 to n does not appear in nums.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"18-2-how-do-we-approach-the-problem-\"><strong>2. How do we approach the problem?<\/strong><\/h3>\n\n\n\n<p>This approach leverages the formula for the sum of the first n natural numbers:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>We know that the sum of all integers from 0 to n is given by the formula:<br><strong>n \u00d7 (n+1) \/ 2<\/strong>\u200b<\/li>\n\n\n\n<li>Therefore, if we compute this <strong>expected sum<\/strong> and then <strong>subtract the actual sum<\/strong> of the elements in nums, the difference will be the missing number.<\/li>\n<\/ol>\n\n\n\n<p>This is a clever trick because it avoids checking each element individually\u2014we just rely on simple arithmetic.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"19-3-implementation-\"><strong>3. Implementation<\/strong><\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def missingNumber(self, nums: List[int]) -&gt; int:\n        n = len(nums)\n        original_total = (n * (n + 1)) \/\/ 2\n        return original_total - sum(nums)\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">missingNumber<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">nums<\/span><span style=\"color: #D4D4D4\">: List[<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">]) -&gt; <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        n = <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(nums)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        original_total = (n * (n + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">)) \/\/ <\/span><span style=\"color: #B5CEA8\">2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> original_total - <\/span><span style=\"color: #DCDCAA\">sum<\/span><span style=\"color: #D4D4D4\">(nums)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"20-4-step-by-step-explanation-\"><strong>4. Step-by-Step Explanation<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Calculate n as the length of nums.<\/li>\n\n\n\n<li>Compute the <strong>expected sum<\/strong> of all numbers from <strong>0 to n<\/strong> using the formula:<br><strong>(n x (n + 1)) \/\/ 2<\/strong><\/li>\n\n\n\n<li>Compute the <strong>actual sum<\/strong> of elements in nums using sum(nums).<\/li>\n\n\n\n<li>Subtract the actual sum from the expected sum to get the <strong>missing number<\/strong>.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"21-5-dry-run-\"><strong>5. Dry Run<\/strong><\/h3>\n\n\n\n<p><strong>Example<\/strong>: Suppose nums = [3, 0, 1].<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>n = 3, which means the numbers should be {0, 1, 2, 3}.<\/li>\n\n\n\n<li>Using the formula: original_total = (n x (n + 1)) \/\/ 2 = 6<\/li>\n\n\n\n<li>The actual sum of nums is 3 + 0 + 1 = 4.<\/li>\n\n\n\n<li>The missing number is 6 &#8211; 4 = 2.<\/li>\n<\/ul>\n\n\n\n<p>Hence, the function returns 2.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"22-6-potential-edge-cases-\"><strong>6. Potential Edge Cases<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Smallest Array:<\/strong>\n<ul class=\"wp-block-list\">\n<li>If nums = [1] (n = 1), then the sum formula is (1*(1+1))\/\/2 = 1, and sum(nums) = 1, so the missing number is 0.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Missing 0:<\/strong>\n<ul class=\"wp-block-list\">\n<li>If nums = [1, 2, 3, \u2026, n], the formula result minus sum(nums) will give 0.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Missing n:<\/strong>\n<ul class=\"wp-block-list\">\n<li>If nums = [0, 1, 2, \u2026, n-1], the difference will yield n.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Handling Larger n:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Since we only use arithmetic operations, even large <strong>n<\/strong> should pose no major issue (assuming no overflow constraints beyond standard Python integers).<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"23-7-time-and-space-complexity-\"><strong>7. Time and Space Complexity<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity<\/strong>: O(n) to compute sum(nums). The arithmetic operations themselves are O(1).<\/li>\n\n\n\n<li><strong>Space Complexity<\/strong>: O(1), since we only store a few numeric variables.<\/li>\n<\/ul>\n\n\n\n<p>For any changes to the document, kindly email at <a href=\"mailto:code@codeanddebug.in\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\">+91-9712928220<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given an array&nbsp;nums&nbsp;containing&nbsp;n&nbsp;distinct numbers in the range&nbsp;[0, n], return&nbsp;the only number in the range that is missing from the array. Here&#8217;s the [Problem Link] to begin with. Example 1:Input: nums = [3,0,1]Output: 2Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in<\/p>\n","protected":false},"author":1,"featured_media":366,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ub_ctt_via":"","footnotes":""},"categories":[3,4],"tags":[7,8,10],"class_list":{"0":"post-360","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-beginner","9":"tag-array","10":"tag-easy","11":"tag-math"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/missing-number-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=360"}],"version-history":[{"count":2,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/360\/revisions"}],"predecessor-version":[{"id":558,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/360\/revisions\/558"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/366"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}