-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay33.java
More file actions
39 lines (30 loc) · 1.07 KB
/
Day33.java
File metadata and controls
39 lines (30 loc) · 1.07 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
/*Array Duplicates
Given an array arr[] of size n, containing elements from the range 1 to n, and each element appears at most twice, return an array of all the integers that appears twice.
Note: You can return the elements in any order but the driver code will print them in sorted order.
Examples:
Input: arr[] = [2, 3, 1, 2, 3]
Output: [2, 3]
Explanation: 2 and 3 occur more than once in the given array.
Input: arr[] = [3, 1, 2]
Output: []
Explanation: There is no repeating element in the array, so the output is empty. */
/* import java.util.*;
class Solution {
public ArrayList<Integer> findDuplicates(int[] arr) {
ArrayList<Integer> result = new ArrayList<>();
int n = arr.length;
for (int i = 0; i < n; i++) {
int index = Math.abs(arr[i]) - 1;
if (arr[index] < 0) {
result.add(index + 1);
} else {
arr[index] = -arr[index];
}
}
return result;
}
}
*/
/*⏱ Time & Space Complexity
Time Complexity: O(n)
Space Complexity: O(1) (excluding output list) */