-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem no. 448.java
48 lines (35 loc) · 1.19 KB
/
Problem no. 448.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
40
41
42
43
44
45
46
47
// Ques : find all disappeared numbers in an array
// Ques Link : https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/
package com.nitin;
import java.util.*;
public class FindAllMissing {
public static void main(String[] args) {
int[] nums = {4,3,2,7,8,2,3,1};
ArrayList<Integer> list = new ArrayList<>(100);
System.out.println(findDisappearedNumbers(nums));
}
public static List<Integer> findDisappearedNumbers(int[] nums) {
int i = 0;
while (i < nums.length) {
int correct = nums[i] - 1;
if (nums[i] != nums[correct]) {
swap(nums, i, correct);
} else {
i++;
}
}
// just find missing numbers
List<Integer> ans = new ArrayList<>();
for (int index = 0; index < nums.length; index++) {
if (nums[index] != index + 1) {
ans.add(index + 1);
}
}
return ans;
}
static void swap(int[] nums, int first, int second) {
int temp = nums[first];
nums[first] = nums[second];
nums[second] = temp;
}
}