forked from super30admin/Array-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem1.java
39 lines (31 loc) · 1.17 KB
/
Problem1.java
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
// Time Complexity :O(n)- One Iteration
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :Yes
// Any problem you faced while coding this :
// Your code here along with comments explaining your approach
import java.util.*;
import java.io.*;
class Solution_1 {
public static List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> result = new ArrayList<>();
int index=0;
//Iterate over entire array and find indexes of the array and negate it
//The index of the positive elements +1 left in the array, indicates the missing number
for(int i=0; i<nums.length; i++){
index = Math.abs(nums[i])-1;
if(nums[index] >0){
nums[index] = -nums[index];
}
}
for(int i=0; i<nums.length;i++){
if(nums[i] > 0){
result.add(i+1);
}
}
return result;
}
public static void main(String[] args) {
int[] input= {4,3,2,7,8,2,3,1};
System.out.println("Missing elements in array: "+findDisappearedNumbers(input) );
}
}