{"id":529,"date":"2025-07-07T19:06:09","date_gmt":"2025-07-07T13:36:09","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=529"},"modified":"2025-07-07T19:06:11","modified_gmt":"2025-07-07T13:36:11","slug":"missing-and-repeating","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/missing-and-repeating\/","title":{"rendered":"Missing And\u00a0Repeating \u2013 GeeksforGeeks Solution\u00a0Explained"},"content":{"rendered":"\n<p>If you\u2019re preparing\u00a0for coding interviews, the &#8220;Missing And\u00a0Repeating&#8221; problem\u00a0is a classic! In this blog, we\u2019ll explain\u00a0the problem, walk\u00a0you through brute\u00a0force, better, and optimal\u00a0solutions, and\u00a0make everything\u00a0easy to understand\u00a0with code comments, dry runs, and\u00a0clear explanations.<\/p>\n\n\n\n<p>Given an unsorted array&nbsp;<strong><em>arr<\/em><\/strong>&nbsp;of positive integers. One number&nbsp;<strong>a<\/strong>&nbsp;from the set&nbsp;<strong>[1, 2,&#8230;.,n]<\/strong>&nbsp;is missing and one number&nbsp;<strong>b<\/strong>&nbsp;occurs twice in the array. Find numbers&nbsp;<strong>a<\/strong>&nbsp;and&nbsp;<strong>b<\/strong>.<\/p>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;The test cases are generated such that there always exists one missing and one repeating number within the range&nbsp;<strong>[1,n]<\/strong>.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/www.geeksforgeeks.org\/problems\/find-missing-and-repeating2512\/1\" 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<p><strong>Examples:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input: <\/strong>arr[] = [2, 2]<br><strong>Output:<\/strong> [2, 1]<br><strong>Explanation:<\/strong> Repeating number is 2 and smallest positive missing number is 1.<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input: <\/strong>arr[] = [1, 3, 3] \n<strong>Output:<\/strong> [3, 2]\n<strong>Explanation:<\/strong> Repeating number is 3 and smallest positive missing number is 2.<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input: <\/strong>arr[] = [4, 3, 6, 2, 1, 1]<br><strong>Output:<\/strong> [1, 5]<br><strong>Explanation:<\/strong> Repeating number is 1 and the missing number is 5.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><br>2 \u2264 arr.size() \u2264 10<sup>6<\/sup><br>1 \u2264 arr[i] \u2264 arr.size()<\/p>\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-1506fc5d-51e2-45ba-aefe-4c1b1fe541ee\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"false\" 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\/missing-and-repeating\/#0-brute-force-solution-nested-loops\" style=\"\">Brute Force Solution (Nested Loops)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/missing-and-repeating\/#1-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/missing-and-repeating\/#2-code-implementation\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/missing-and-repeating\/#3-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/missing-and-repeating\/#4-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/missing-and-repeating\/#5-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/missing-and-repeating\/#6-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/missing-and-repeating\/#7-better-solution-hashing\" style=\"\">Better Solution (Hashing)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/missing-and-repeating\/#8-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/missing-and-repeating\/#9-code-implementation\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/missing-and-repeating\/#10-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/missing-and-repeating\/#11-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/missing-and-repeating\/#12-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/missing-and-repeating\/#13-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/missing-and-repeating\/#14-optimal-solution-maths-sum-and-sum-of-squares\" style=\"\">Optimal Solution (Maths: Sum and Sum of Squares)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/missing-and-repeating\/#15-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/missing-and-repeating\/#16-code-implementation\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/missing-and-repeating\/#17-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/missing-and-repeating\/#18-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/missing-and-repeating\/#19-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/missing-and-repeating\/#20-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/missing-and-repeating\/#21-final-thoughts\" style=\"\">Final Thoughts<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"0-brute-force-solution-nested-loops\">Brute Force Solution (Nested Loops)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p>Let\u2019s solve the problem in the simplest way:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Count Occurrences:<\/strong><br>For each number from 1 to n, count how many times it appears in the array.<\/li>\n\n\n\n<li><strong>Identify Repeating and Missing:<\/strong>\n<ul class=\"wp-block-list\">\n<li>If a number appears twice, it\u2019s the repeating number.<\/li>\n\n\n\n<li>If a number doesn\u2019t appear at all, it\u2019s the missing number.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Why does this work?<\/strong><br>We check every number, so we\u2019re sure to find both the missing and repeating numbers.<\/li>\n<\/ol>\n\n\n\n<p>This approach is easy to understand but not efficient for large arrays.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-code-implementation\">Code Implementation<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" 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;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);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 findTwoElement(self, arr):\n        n = len(arr)\n        repeating, missing = -1, -1\n\n        for i in range(1, n + 1):\n            count = 0\n            for j in range(n):\n                if arr[j] == i:\n                    count += 1\n\n            if count == 2:\n                repeating = i\n            elif count == 0:\n                missing = i\n\n            if repeating != -1 and missing != -1:\n                break\n\n        return [repeating, missing]\" 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\">findTwoElement<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">arr<\/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\">(arr)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        repeating, missing = -<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, -<\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><\/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\">1<\/span><span style=\"color: #D4D4D4\">, n + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            count = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> j <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(n):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> arr[j] == i:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    count += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> count == <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                repeating = i<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">elif<\/span><span style=\"color: #D4D4D4\"> count == <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                missing = i<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> repeating != -<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">and<\/span><span style=\"color: #D4D4D4\"> missing != -<\/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\">break<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> [repeating, missing]<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>For each number from 1 to n, we count how many times it appears in the array.<\/li>\n\n\n\n<li>If a number appears twice, we store it as\u00a0<code>repeating<\/code>.<\/li>\n\n\n\n<li>If a number doesn\u2019t appear at all, we store it as\u00a0<code>missing<\/code>.<\/li>\n\n\n\n<li>As soon as we find both, we return the answer.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-dry-run\">Dry Run<\/h3>\n\n\n\n<p><strong>Input:<\/strong><br><code>arr = [1][1]<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Check 1: appears 2 times \u2192 repeating = 1<\/li>\n\n\n\n<li>Check 2: appears 1 time<\/li>\n\n\n\n<li>Check 3: appears 1 time<\/li>\n\n\n\n<li>Check 4: appears 1 time<\/li>\n\n\n\n<li>Check 5: appears 0 times \u2192 missing = 5<\/li>\n\n\n\n<li>Check 6: appears 1 time<\/li>\n<\/ul>\n\n\n\n<p><strong>Output:<\/strong><br><code>[1]<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\u00a0O(n^2)<br>Two nested loops: for each number, check the whole array.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\u00a0O(1)<br>Only a few variables are used.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-conclusion\">Conclusion<\/h3>\n\n\n\n<p>The brute force approach is simple and good for understanding, but it\u2019s too slow for large arrays.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"7-better-solution-hashing\">Better Solution (Hashing)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p>Let\u2019s make it faster using extra space:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Create a Hash Array:<\/strong><br>Make an array to count how many times each number appears.<\/li>\n\n\n\n<li><strong>Scan for Repeating and Missing:<\/strong>\n<ul class=\"wp-block-list\">\n<li>If a number appears twice, it\u2019s the repeating number.<\/li>\n\n\n\n<li>If a number doesn\u2019t appear, it\u2019s the missing number.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Why does this work?<\/strong><br>Counting with a hash array is much faster than nested loops.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-code-implementation\">Code Implementation<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" 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;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);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 findTwoElement(self, arr):\n        n = len(arr)\n        repeating, missing = -1, -1\n\n        hash_list = [0] * (n + 1)              # create a hash list to count occurrences\n        for num in arr:\n            hash_list[num] += 1                # count each number\n\n        for i in range(1, len(hash_list)):\n            if hash_list[i] == 2:              # found repeating number\n                repeating = i\n            elif hash_list[i] == 0:            # found missing number\n                missing = i\n            if repeating != -1 and missing != -1:\n                return [repeating, missing]\" 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\">findTwoElement<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">arr<\/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\">(arr)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        repeating, missing = -<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, -<\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        hash_list = [<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">] * (n + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">)              <\/span><span style=\"color: #6A9955\"># create a hash list to count occurrences<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> num <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> arr:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            hash_list[num] += <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #6A9955\"># count each number<\/span><\/span>\n<span class=\"line\"><\/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\">1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(hash_list)):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> hash_list[i] == <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">:              <\/span><span style=\"color: #6A9955\"># found repeating number<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                repeating = i<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">elif<\/span><span style=\"color: #D4D4D4\"> hash_list[i] == <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">:            <\/span><span style=\"color: #6A9955\"># found missing number<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                missing = i<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> repeating != -<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">and<\/span><span style=\"color: #D4D4D4\"> missing != -<\/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\">return<\/span><span style=\"color: #D4D4D4\"> [repeating, missing]<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"10-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We use a hash array to count how many times each number appears.<\/li>\n\n\n\n<li>If a number appears twice, it\u2019s repeating.<\/li>\n\n\n\n<li>If a number doesn\u2019t appear, it\u2019s missing.<\/li>\n\n\n\n<li>As soon as we find both, we return the answer.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11-dry-run\">Dry Run<\/h3>\n\n\n\n<p><strong>Input:<\/strong><br><code>arr = [1][1]<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>hash_list after counting:\u00a0<code>[1][1][1][1]<\/code><\/li>\n\n\n\n<li>1 appears 2 times \u2192 repeating = 1<\/li>\n\n\n\n<li>5 appears 0 times \u2192 missing = 5<\/li>\n<\/ul>\n\n\n\n<p><strong>Output:<\/strong><br><code>[1]<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"12-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\u00a0O(n)<br>One pass to count, one pass to check.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\u00a0O(n)<br>Extra space for the hash array.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"13-conclusion\">Conclusion<\/h3>\n\n\n\n<p>The better solution is much faster and is a good choice for medium-sized arrays.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"14-optimal-solution-maths-sum-and-sum-of-squares\">Optimal Solution (Maths: Sum and Sum of Squares)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"15-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p>Let\u2019s solve the problem with no extra space and in just one pass:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Maths Approach:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Let the missing number be\u00a0<code>y<\/code>\u00a0and the repeating number be\u00a0<code>x<\/code>.<\/li>\n\n\n\n<li>Calculate the sum and sum of squares of the first n natural numbers.<\/li>\n\n\n\n<li>Calculate the sum and sum of squares of the array.<\/li>\n\n\n\n<li>Use these two equations:\n<ul class=\"wp-block-list\">\n<li><code>x - y = sum(arr) - sum(1...n)<\/code><\/li>\n\n\n\n<li><code>x^2 - y^2 = sum_of_squares(arr) - sum_of_squares(1...n)<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Solve the Equations:<\/strong>\n<ul class=\"wp-block-list\">\n<li>From above, calculate\u00a0<code>x + y<\/code>\u00a0and then find\u00a0<code>x<\/code>\u00a0and\u00a0<code>y<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Why does this work?<\/strong><br>The difference in sums and squares gives us two equations to solve for the missing and repeating numbers.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"16-code-implementation\">Code Implementation<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" 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;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);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 findTwoElement(self, arr):\n        n = len(arr)\n        sN = (n * (n + 1)) \/\/ 2                      # sum of first n numbers\n        s2N = (n * (n + 1) * (2 * n + 1)) \/\/ 6       # sum of squares of first n numbers\n\n        s = 0\n        s2 = 0\n        for num in arr:\n            s += num                                 # sum of array\n            s2 += num**2                             # sum of squares of array\n\n        val1 = s - sN                                # x - y\n        val2 = s2 - s2N                              # x^2 - y^2\n\n        val2 = val2 \/\/ val1                          # (x^2 - y^2) \/\/ (x - y) = x + y\n        x = (val1 + val2) \/\/ 2                       # repeating number\n        y = x - val1                                 # missing number\n\n        return [x, y]\" 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\">findTwoElement<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">arr<\/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\">(arr)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        sN = (n * (n + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">)) \/\/ <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">                      <\/span><span style=\"color: #6A9955\"># sum of first n numbers<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        s2N = (n * (n + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">) * (<\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\"> * n + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">)) \/\/ <\/span><span style=\"color: #B5CEA8\">6<\/span><span style=\"color: #D4D4D4\">       <\/span><span style=\"color: #6A9955\"># sum of squares of first n numbers<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        s = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        s2 = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> num <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> arr:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            s += num                                 <\/span><span style=\"color: #6A9955\"># sum of array<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            s2 += num**<\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">                             <\/span><span style=\"color: #6A9955\"># sum of squares of array<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        val1 = s - sN                                <\/span><span style=\"color: #6A9955\"># x - y<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        val2 = s2 - s2N                              <\/span><span style=\"color: #6A9955\"># x^2 - y^2<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        val2 = val2 \/\/ val1                          <\/span><span style=\"color: #6A9955\"># (x^2 - y^2) \/\/ (x - y) = x + y<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        x = (val1 + val2) \/\/ <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">                       <\/span><span style=\"color: #6A9955\"># repeating number<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        y = x - val1                                 <\/span><span style=\"color: #6A9955\"># missing number<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> [x, y]<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"17-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Calculate expected sum and sum of squares for numbers 1 to n.<\/li>\n\n\n\n<li>Calculate actual sum and sum of squares for the given array.<\/li>\n\n\n\n<li>Use the differences to set up equations and solve for missing and repeating numbers.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"18-dry-run\">Dry Run<\/h3>\n\n\n\n<p><strong>Input:<\/strong><br><code>arr = [1][1]<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>n = 6<\/li>\n\n\n\n<li>sN = 21, s2N = 91<\/li>\n\n\n\n<li>s = 17, s2 = 63<\/li>\n\n\n\n<li>val1 = s &#8211; sN = -4<\/li>\n\n\n\n<li>val2 = s2 &#8211; s2N = -28<\/li>\n\n\n\n<li>val2 \/\/ val1 = 7<\/li>\n\n\n\n<li>x = (val1 + val2) \/\/ 2 = (\u20134 + 7) \/\/ 2 = 1<\/li>\n\n\n\n<li>y = x \u2013 val1 = 1 \u2013 (\u20134) = 5<\/li>\n<\/ul>\n\n\n\n<p><strong>Output:<\/strong><br><code>[1]<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"19-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\u00a0O(n)<br>Only one pass for sums.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\u00a0O(1)<br>Only variables, no extra arrays.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"20-conclusion\">Conclusion<\/h3>\n\n\n\n<p>The optimal solution is fast and uses no extra space. It\u2019s the best approach for interviews and large datasets.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"21-final-thoughts\">Final Thoughts<\/h2>\n\n\n\n<p>The &#8220;Missing And Repeating&#8221; problem is a great example of how to optimize your solution step by step. Start with brute force to understand the basics, then use hashing for speed, and finally use maths for the most efficient answer.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Join our Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\" target=\"_blank\" rel=\"noreferrer noopener\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\" target=\"_blank\" rel=\"noreferrer noopener\">+91-9712928220<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you\u2019re preparing\u00a0for coding interviews, the &#8220;Missing And\u00a0Repeating&#8221; problem\u00a0is a classic! In this blog, we\u2019ll explain\u00a0the problem, walk\u00a0you through brute\u00a0force, better, and optimal\u00a0solutions, and\u00a0make everything\u00a0easy to understand\u00a0with code comments, dry runs, and\u00a0clear explanations. Given an unsorted array&nbsp;arr&nbsp;of positive integers. One number&nbsp;a&nbsp;from the set&nbsp;[1, 2,&#8230;.,n]&nbsp;is missing and one number&nbsp;b&nbsp;occurs twice in the array. Find numbers&nbsp;a&nbsp;and&nbsp;b. Note:&nbsp;The<\/p>\n","protected":false},"author":1,"featured_media":530,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,5],"tags":[7,18],"class_list":{"0":"post-529","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-expert","9":"tag-array","10":"tag-hard"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/07\/missing-and-repeating-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\/529","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=529"}],"version-history":[{"count":1,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/529\/revisions"}],"predecessor-version":[{"id":531,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/529\/revisions\/531"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/530"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}