Skip to content

Commit aac0a7c

Browse files
authored
Merge pull request codeharborhub#2932 from Ishitamukherjee2004/main
Solution of 268 (LC No.) is added
2 parents 424b4d3 + ff03596 commit aac0a7c

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
id: missing-number
3+
title: Missing Number
4+
sidebar_label: 268-Missing-Number
5+
tags:
6+
- Array
7+
- Hash Table
8+
- Math
9+
- Binary search
10+
- Bit Manupulation
11+
- Sorting
12+
13+
---
14+
15+
## Problem Description
16+
17+
Given an array `nums` containing `n` distinct numbers in the range `[0, n]`, return the only number in the range that is missing from the array.
18+
19+
20+
### Example
21+
22+
**Example 1:**
23+
24+
```
25+
Input: nums = [3,0,1]
26+
Output: 2
27+
Explanation: 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.
28+
```
29+
30+
**Example 2:**
31+
```
32+
Input: nums = [0,1]
33+
Output: 2
34+
Explanation: 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.
35+
```
36+
37+
### Constraints
38+
39+
- `1 <= n <= 104`
40+
41+
## Solution Approach
42+
43+
### Intuition:
44+
45+
To efficiently determine the missing number in the array
46+
47+
48+
## Solution Implementation
49+
50+
### Code (C++):
51+
52+
```cpp
53+
class Solution {
54+
public:
55+
int missingNumber(vector<int>& nums) {
56+
int n = nums.size();
57+
int sum = (n*(n+1))/2;
58+
int s1=0;
59+
for(int i=0; i<n; i++){
60+
s1+=nums[i];
61+
}
62+
int ans = sum-s1;
63+
return ans;
64+
}
65+
};
66+
67+
```

0 commit comments

Comments
 (0)