-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay37.java
More file actions
54 lines (44 loc) · 1.49 KB
/
Copy pathDay37.java
File metadata and controls
54 lines (44 loc) · 1.49 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
52
53
54
/*Missing And Repeating
Given an unsorted array arr[] of size n, containing elements from the range 1 to n, it is known that one number in this range is missing, and another number occurs twice in the array, find both the duplicate number and the missing number.
Examples:
Input: arr[] = [2, 2]
Output: [2, 1]
Explanation: Repeating number is 2 and the missing number is 1.
Input: arr[] = [1, 3, 3]
Output: [3, 2]
Explanation: Repeating number is 3 and the missing number is 2.
Input: arr[] = [4, 3, 6, 2, 1, 1]
Output: [1, 5]
Explanation: Repeating number is 1 and the missing number is 5. */
/* import java.util.*;
class Solution {
ArrayList<Integer> findTwoElement(int arr[]) {
int n = arr.length;
int repeating = -1, missing = -1;
// Step 1: Find the repeating number
for (int i = 0; i < n; i++) {
int idx = Math.abs(arr[i]) - 1;
if (arr[idx] < 0) {
repeating = idx + 1;
} else {
arr[idx] = -arr[idx];
}
}
// Step 2: Find the missing number
for (int i = 0; i < n; i++) {
if (arr[i] > 0) {
missing = i + 1;
break;
}
}
// Result: [repeating, missing]
ArrayList<Integer> result = new ArrayList<>();
result.add(repeating);
result.add(missing);
return result;
}
}
*/
/* ⏱️ Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1) (excluding output list)*/