-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMissing number in array.cpp
More file actions
50 lines (38 loc) · 917 Bytes
/
Missing number in array.cpp
File metadata and controls
50 lines (38 loc) · 917 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
Given an array of size N-1 such that it only contains distinct integers in the range of 1 to N. Find the missing element.
Example 1:
Input:
N = 5
A[] = {1,2,3,5}
Output: 4
Example 2:
Input:
N = 10
A[] = {6,1,2,8,3,4,7,10,5}
Output: 9
Your Task :
You don't need to read input or print anything. Complete the function MissingNumber() that takes array and N as input parameters and returns the value of the missing number.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 106
1 ≤ A[i] ≤ 106
*/
#include <bits/stdc++.h>
using namespace std;
// Function to get the missing number
int getMissingNo(int a[], int n)
{
int total = (n + 1) * (n + 2) / 2;
for (int i = 0; i < n; i++)
total -= a[i];
return total;
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 4, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
int miss = getMissingNo(arr, n);
cout << miss;
}