Skip to content

Commit 5056e6d

Browse files
authored
Merge pull request #2023 from arunimaChintu/patch-2
Adds gfg soln "missing-number-in-array1416"
2 parents 8652a37 + ee53276 commit 5056e6d

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
---
2+
id: find-the-missing-number
3+
title: Find the Missing Number Problem (gfg)
4+
sidebar_label: 0005 - 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 website, featuring implementations in Python and C++."
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="@YourUsername"/>
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="@YourUsername"/>
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+
</Tabs>
126+
127+
## Example Walkthrough
128+
129+
For the array `array = [3, 1, 4]` with `n = 4`:
130+
131+
1. The expected sum of the first 4 natural numbers is `4 * (4 + 1) / 2 = 10`.
132+
2. The sum of the array elements is `3 + 1 + 4 = 8`.
133+
3. The missing number is `10 - 8 = 2`.
134+
135+
For the array `array = [5, 3, 1, 2]` with `n = 5`:
136+
137+
1. The expected sum of the first 5 natural numbers is `5 * (5 + 1) / 2 = 15`.
138+
2. The sum of the array elements is `5 + 3 + 1 + 2 = 11`.
139+
3. The missing number is `15 - 11 = 4`.
140+
141+
## Solution Logic:
142+
143+
1. Calculate the expected sum of the first `n` natural numbers using the formula `n * (n + 1) / 2`.
144+
2. Calculate the sum of the elements in the given array.
145+
3. The missing number is the difference between the expected sum and the sum of the array elements.
146+
147+
## Time Complexity
148+
149+
* The time complexity is $O(n)$, where n is the size of the input array.
150+
151+
## Space Complexity
152+
153+
* The auxiliary space complexity is $O(1)$ because we are not using any extra space proportional to the size of the input array.
154+
155+
## References
156+
157+
- **gfg Problem:** [gfg Problem](https://www.geeksforgeeks.org/problems/missing-number-in-array1416/1?page=1&difficulty=Easy&sortBy=submissions)
158+
- **Solution Author:** [arunimad6yuq](https://www.geeksforgeeks.org/user/arunimad6yuq/)

0 commit comments

Comments
 (0)