forked from super30admin/Array-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PR1
32 lines (26 loc) · 1.07 KB
/
PR1
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
// Time Complexity : O(2n) -> O(n)
// Space Complexity : O(1) - > no extra space.
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No
// Your code here along with comments explaining your approach : Used '-' as a identifier to a particular index value of the number in the array, to understand if that element is
present. Iterate over the array again and check if there is a positive number, then add that position value to the array.
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> result = new ArrayList<Integer>();
for(int i=0;i<nums.length;i++){
int num= Math.abs(nums[i]);
if(nums[num-1]<0){
continue;
}
else{
nums[num-1]=-nums[num-1];
}
}
for(int i=0;i<nums.length;i++){
if(nums[i]>0){
result.add(i+1);
}
}
return result;
}
}