|
| 1 | +--- |
| 2 | +id: find-the-missing-number |
| 3 | +title: Find the Missing Number Problem |
| 4 | +sidebar_label: Find-The-Missing-Number |
| 5 | +tags: |
| 6 | + - Intermediate |
| 7 | + - Array |
| 8 | + - Mathematical |
| 9 | + - GeeksforGeeks |
| 10 | + - CPP |
| 11 | + - Python |
| 12 | + - DSA |
| 13 | +description: "This tutorial covers the solution to the Find the Missing Number problem from the GeeksforGeeks." |
| 14 | +--- |
| 15 | +## Problem Description |
| 16 | + |
| 17 | +Given an array containing `n-1` distinct numbers taken from 1 to `n`, find the one number that is missing from the array. |
| 18 | + |
| 19 | +## Examples |
| 20 | + |
| 21 | +**Example 1:** |
| 22 | + |
| 23 | +``` |
| 24 | +Input: array = [3, 1, 4] |
| 25 | +Output: 2 |
| 26 | +Explanation: The missing number is 2. |
| 27 | +``` |
| 28 | + |
| 29 | +**Example 2:** |
| 30 | + |
| 31 | +``` |
| 32 | +Input: array = [5, 3, 1, 2] |
| 33 | +Output: 4 |
| 34 | +Explanation: The missing number is 4. |
| 35 | +``` |
| 36 | + |
| 37 | +## Your Task |
| 38 | + |
| 39 | +You don't need to read input or print anything. Your task is to complete the function `missingNumber()` which takes the size `n` of the range and the array `arr` as inputs and returns the missing number. |
| 40 | + |
| 41 | +Expected Time Complexity: $O(n)$ |
| 42 | + |
| 43 | +Expected Auxiliary Space: $O(1)$ |
| 44 | + |
| 45 | +## Constraints |
| 46 | + |
| 47 | +* `2 ≤ n ≤ 10^6` |
| 48 | +* `1 ≤ array[i] ≤ n` |
| 49 | +* All elements of the array are distinct. |
| 50 | + |
| 51 | +## Problem Explanation |
| 52 | + |
| 53 | +The problem is to find the missing number from an array of `n-1` elements which contains distinct numbers from 1 to `n`. The missing number is the one number that is not present in the array. |
| 54 | + |
| 55 | +## Code Implementation |
| 56 | + |
| 57 | +<Tabs> |
| 58 | + <TabItem value="Python" label="Python" default> |
| 59 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 60 | + |
| 61 | + ```py |
| 62 | + class Solution: |
| 63 | + def missingNumber(self, n, arr): |
| 64 | + # Calculate the expected sum of the first n natural numbers |
| 65 | + total_sum = n * (n + 1) // 2 |
| 66 | + |
| 67 | + # Calculate the sum of the elements in the array |
| 68 | + arr_sum = sum(arr) |
| 69 | + |
| 70 | + # The missing number is the difference between the expected sum and the array sum |
| 71 | + return total_sum - arr_sum |
| 72 | + |
| 73 | + # Example usage |
| 74 | + if __name__ == "__main__": |
| 75 | + solution = Solution() |
| 76 | + print(solution.missingNumber(4, [3, 1, 4])) # Expected output: 2 |
| 77 | + print(solution.missingNumber(5, [5, 3, 1, 2])) # Expected output: 4 |
| 78 | + ``` |
| 79 | + |
| 80 | + </TabItem> |
| 81 | + <TabItem value="C++" label="C++"> |
| 82 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 83 | + |
| 84 | + ```cpp |
| 85 | + #include <iostream> |
| 86 | + #include <vector> |
| 87 | + using namespace std; |
| 88 | + |
| 89 | + class Solution { |
| 90 | + public: |
| 91 | + int missingNumber(int n, vector<int>& arr) { |
| 92 | + // Calculate the expected sum of the first n natural numbers |
| 93 | + int total_sum = n * (n + 1) / 2; |
| 94 | + |
| 95 | + // Calculate the sum of the elements in the array |
| 96 | + int arr_sum = 0; |
| 97 | + for (int num : arr) { |
| 98 | + arr_sum += num; |
| 99 | + } |
| 100 | + |
| 101 | + // The missing number is the difference between the expected sum and the array sum |
| 102 | + return total_sum - arr_sum; |
| 103 | + } |
| 104 | + }; |
| 105 | + |
| 106 | + // Example usage |
| 107 | + int main() { |
| 108 | + int t; |
| 109 | + cin >> t; |
| 110 | + while (t--) { |
| 111 | + int n; |
| 112 | + cin >> n; |
| 113 | + |
| 114 | + vector<int> arr(n - 1); |
| 115 | + for (int i = 0; i < n - 1; ++i) |
| 116 | + cin >> arr[i]; |
| 117 | + Solution obj; |
| 118 | + cout << obj.missingNumber(n, arr) << "\n"; |
| 119 | + } |
| 120 | + return 0; |
| 121 | + } |
| 122 | + ``` |
| 123 | + |
| 124 | + </TabItem> |
| 125 | + |
| 126 | + <TabItem value="Javascript" label="Javascript" default> |
| 127 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 128 | + |
| 129 | + ```javascript |
| 130 | + class Solution { |
| 131 | + missingNumber(n, arr) { |
| 132 | + let totalSum = n * (n + 1) / 2; |
| 133 | + let arrSum = arr.reduce((a, b) => a + b, 0); |
| 134 | + return totalSum - arrSum; |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +let solution = new Solution(); |
| 139 | +console.log(solution.missingNumber(4, [3, 1, 4])); // Expected output: 2 |
| 140 | +console.log(solution.missingNumber(5, [5, 3, 1, 2])); // Expected output: 4 |
| 141 | + |
| 142 | + ``` |
| 143 | + |
| 144 | + </TabItem> |
| 145 | + |
| 146 | + <TabItem value="Typescript" label="Typescript" default> |
| 147 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 148 | + |
| 149 | + ```typescript |
| 150 | + class Solution { |
| 151 | + missingNumber(n: number, arr: number[]): number { |
| 152 | + let totalSum: number = n * (n + 1) / 2; |
| 153 | + let arrSum: number = arr.reduce((a, b) => a + b, 0); |
| 154 | + return totalSum - arrSum; |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +let solution: Solution = new Solution(); |
| 159 | +console.log(solution.missingNumber(4, [3, 1, 4])); // Expected output: 2 |
| 160 | +console.log(solution.missingNumber(5, [5, 3, 1, 2])); // Expected output: 4 |
| 161 | + |
| 162 | + ``` |
| 163 | + |
| 164 | + </TabItem> |
| 165 | + |
| 166 | + <TabItem value="Java" label="Java" default> |
| 167 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 168 | + |
| 169 | + ```java |
| 170 | + public class Solution { |
| 171 | + public int missingNumber(int n, int[] arr) { |
| 172 | + int totalSum = n * (n + 1) / 2; |
| 173 | + int arrSum = 0; |
| 174 | + for (int num : arr) { |
| 175 | + arrSum += num; |
| 176 | + } |
| 177 | + return totalSum - arrSum; |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +public class Main { |
| 182 | + public static void main(String[] args) { |
| 183 | + Solution solution = new Solution(); |
| 184 | + System.out.println(solution.missingNumber(4, new int[]{3, 1, 4})); // Expected output: 2 |
| 185 | + System.out.println(solution.missingNumber(5, new int[]{5, 3, 1, 2})); // Expected output: 4 |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | + ``` |
| 190 | + |
| 191 | + </TabItem> |
| 192 | +</Tabs> |
| 193 | + |
| 194 | +## Example Walkthrough |
| 195 | + |
| 196 | +For the array `array = [3, 1, 4]` with `n = 4`: |
| 197 | + |
| 198 | +1. The expected sum of the first 4 natural numbers is `4 * (4 + 1) / 2 = 10`. |
| 199 | +2. The sum of the array elements is `3 + 1 + 4 = 8`. |
| 200 | +3. The missing number is `10 - 8 = 2`. |
| 201 | + |
| 202 | +For the array `array = [5, 3, 1, 2]` with `n = 5`: |
| 203 | + |
| 204 | +1. The expected sum of the first 5 natural numbers is `5 * (5 + 1) / 2 = 15`. |
| 205 | +2. The sum of the array elements is `5 + 3 + 1 + 2 = 11`. |
| 206 | +3. The missing number is `15 - 11 = 4`. |
| 207 | + |
| 208 | +## Solution Logic: |
| 209 | + |
| 210 | +1. Calculate the expected sum of the first `n` natural numbers using the formula `n * (n + 1) / 2`. |
| 211 | +2. Calculate the sum of the elements in the given array. |
| 212 | +3. The missing number is the difference between the expected sum and the sum of the array elements. |
| 213 | + |
| 214 | +## Time Complexity |
| 215 | + |
| 216 | +* The time complexity is $O(n)$, where n is the size of the input array. |
| 217 | + |
| 218 | +## Space Complexity |
| 219 | + |
| 220 | +* The auxiliary space complexity is $O(1)$ because we are not using any extra space proportional to the size of the input array. |
0 commit comments