-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay43.java
More file actions
51 lines (41 loc) · 1.2 KB
/
Copy pathDay43.java
File metadata and controls
51 lines (41 loc) · 1.2 KB
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
51
/*Missing in Array
You are given an array arr[] of size n - 1 that contains distinct integers in the range from 1 to n (inclusive). This array represents a permutation of the integers from 1 to n with one element missing. Your task is to identify and return the missing element.
Examples:
Input: arr[] = [1, 2, 3, 5]
Output: 4
Explanation: All the numbers from 1 to 5 are present except 4.
Input: arr[] = [8, 2, 4, 5, 3, 7, 1]
Output: 6
Explanation: All the numbers from 1 to 8 are present except 6.
Input: arr[] = [1]
Output: 2
Explanation: Only 1 is present so the missing element is 2. */
/*class Solution {
int missingNum(int arr[]) {
int n = arr.length + 1;
long expectedSum = (long) n * (n + 1) / 2;
long actualSum = 0;
for (int num : arr) {
actualSum += num;
}
return (int)(expectedSum - actualSum);
}
}
*/
/*class Solution {
int missingNum(int arr[]) {
int n = arr.length + 1;
int xor = 0;
for (int i = 1; i <= n; i++) {
xor ^= i;
}
for (int num : arr) {
xor ^= num;
}
return xor;
}
}
*/
/*⏱ Complexity
Time Complexity: O(n)
Space Complexity: O(1) */