-
-
Notifications
You must be signed in to change notification settings - Fork 155
Adds gfg soln "missing-number-in-array1416" #2023
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c7f0354
Create missing-number-in-array1416
arunimaChintu d029b7e
Merge branch 'main' into patch-2
ajay-dhangar 9838237
Merge branch 'main' into patch-2
ajay-dhangar d187f63
Merge branch 'main' into patch-2
ajay-dhangar e93abc9
Merge branch 'main' into patch-2
ajay-dhangar d1c481d
Update missing-number-in-array1416
arunimaChintu ee53276
Merge branch 'main' into patch-2
ajay-dhangar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
158 changes: 158 additions & 0 deletions
158
dsa-solutions/gfg-solutions/Easy problems/missing-number-in-array1416
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
--- | ||
id: find-the-missing-number | ||
title: Find the Missing Number Problem (gfg) | ||
sidebar_label: 0005 - Find the Missing Number | ||
tags: | ||
- Intermediate | ||
- Array | ||
- Mathematical | ||
- GeeksforGeeks | ||
- CPP | ||
- Python | ||
- DSA | ||
description: "This tutorial covers the solution to the Find the Missing Number problem from the GeeksforGeeks website, featuring implementations in Python and C++." | ||
--- | ||
## Problem Description | ||
|
||
Given an array containing `n-1` distinct numbers taken from 1 to `n`, find the one number that is missing from the array. | ||
|
||
## Examples | ||
|
||
**Example 1:** | ||
|
||
``` | ||
Input: array = [3, 1, 4] | ||
Output: 2 | ||
Explanation: The missing number is 2. | ||
``` | ||
|
||
**Example 2:** | ||
|
||
``` | ||
Input: array = [5, 3, 1, 2] | ||
Output: 4 | ||
Explanation: The missing number is 4. | ||
``` | ||
|
||
## Your Task | ||
|
||
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. | ||
|
||
Expected Time Complexity: $O(n)$ | ||
|
||
Expected Auxiliary Space: $O(1)$ | ||
|
||
## Constraints | ||
|
||
* `2 ≤ n ≤ 10^6` | ||
* `1 ≤ array[i] ≤ n` | ||
* All elements of the array are distinct. | ||
|
||
## Problem Explanation | ||
|
||
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. | ||
|
||
## Code Implementation | ||
|
||
<Tabs> | ||
<TabItem value="Python" label="Python" default> | ||
<SolutionAuthor name="@YourUsername"/> | ||
|
||
```py | ||
class Solution: | ||
def missingNumber(self, n, arr): | ||
# Calculate the expected sum of the first n natural numbers | ||
total_sum = n * (n + 1) // 2 | ||
|
||
# Calculate the sum of the elements in the array | ||
arr_sum = sum(arr) | ||
|
||
# The missing number is the difference between the expected sum and the array sum | ||
return total_sum - arr_sum | ||
|
||
# Example usage | ||
if __name__ == "__main__": | ||
solution = Solution() | ||
print(solution.missingNumber(4, [3, 1, 4])) # Expected output: 2 | ||
print(solution.missingNumber(5, [5, 3, 1, 2])) # Expected output: 4 | ||
``` | ||
|
||
</TabItem> | ||
<TabItem value="C++" label="C++"> | ||
<SolutionAuthor name="@YourUsername"/> | ||
|
||
```cpp | ||
#include <iostream> | ||
#include <vector> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
int missingNumber(int n, vector<int>& arr) { | ||
// Calculate the expected sum of the first n natural numbers | ||
int total_sum = n * (n + 1) / 2; | ||
|
||
// Calculate the sum of the elements in the array | ||
int arr_sum = 0; | ||
for (int num : arr) { | ||
arr_sum += num; | ||
} | ||
|
||
// The missing number is the difference between the expected sum and the array sum | ||
return total_sum - arr_sum; | ||
} | ||
}; | ||
|
||
// Example usage | ||
int main() { | ||
int t; | ||
cin >> t; | ||
while (t--) { | ||
int n; | ||
cin >> n; | ||
|
||
vector<int> arr(n - 1); | ||
for (int i = 0; i < n - 1; ++i) | ||
cin >> arr[i]; | ||
Solution obj; | ||
cout << obj.missingNumber(n, arr) << "\n"; | ||
} | ||
return 0; | ||
} | ||
``` | ||
|
||
</TabItem> | ||
</Tabs> | ||
|
||
## Example Walkthrough | ||
|
||
For the array `array = [3, 1, 4]` with `n = 4`: | ||
|
||
1. The expected sum of the first 4 natural numbers is `4 * (4 + 1) / 2 = 10`. | ||
2. The sum of the array elements is `3 + 1 + 4 = 8`. | ||
3. The missing number is `10 - 8 = 2`. | ||
|
||
For the array `array = [5, 3, 1, 2]` with `n = 5`: | ||
|
||
1. The expected sum of the first 5 natural numbers is `5 * (5 + 1) / 2 = 15`. | ||
2. The sum of the array elements is `5 + 3 + 1 + 2 = 11`. | ||
3. The missing number is `15 - 11 = 4`. | ||
|
||
## Solution Logic: | ||
|
||
1. Calculate the expected sum of the first `n` natural numbers using the formula `n * (n + 1) / 2`. | ||
2. Calculate the sum of the elements in the given array. | ||
3. The missing number is the difference between the expected sum and the sum of the array elements. | ||
|
||
## Time Complexity | ||
|
||
* The time complexity is $O(n)$, where n is the size of the input array. | ||
|
||
## Space Complexity | ||
|
||
* The auxiliary space complexity is $O(1)$ because we are not using any extra space proportional to the size of the input array. | ||
|
||
## References | ||
|
||
- **gfg Problem:** [gfg Problem](https://www.geeksforgeeks.org/problems/missing-number-in-array1416/1?page=1&difficulty=Easy&sortBy=submissions) | ||
- **Solution Author:** [arunimad6yuq](https://www.geeksforgeeks.org/user/arunimad6yuq/) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.