-
Notifications
You must be signed in to change notification settings - Fork 2
/
duplicateNumber.cpp
39 lines (34 loc) · 987 Bytes
/
duplicateNumber.cpp
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
// arr - input array
// size - size of array
/*
Given an array of integers of size n which contains numbers from 0 to n - 2.
Each number is present at least once. That is,
if n = 5, numbers from 0 to 3 is present in the given array at least once and one number is present twice.
You need to find and return that duplicate number present in the array.
Assume, duplicate number is always present in the array.
Input format :
Line 1 : Size of input array
Line 2 : Array elements (separated by space)
Output Format :
Duplicate element
Constraints :
1 <= n <= 10^6
Sample Input:
9
0 7 2 5 4 7 1 3 6
Sample Output:
7
*/
int MissingNumber(int arr[], int n){
/* Don't write main().
* Don't read input, it is passed as function argument.
* Return output and don't print it.
* Taking input and printing output is handled automatically.
*/
int sum =0;
for(int i=0; i<n; i++){
sum += arr[i];
}
int s = (n-2)*(n-1)/2;
return sum - s;
}