Find All Numbers Disappeared in an Array LeetCode solution in Java
1class Solution {2public List<Integer> findDisappearedNumbers(int[] nums) {3List<Integer> res = new ArrayList<>();4// 0 1 2 3 4 5 6 7 <- indx5// 4 3 2 7 8 2 3 1 <- nums[i]6for (int i = 0; i < nums.length; i++) {7int indx = Math.abs(nums[i]) - 1;8if (nums[indx] > 0) {9nums[indx] = nums[indx] * -1;10}11}12// 0 1 2 3 4 5 6 7 <- indx13// -4 -3 -2 -7 8 2 -3 -1 <- nums[i]14for (int i = 0; i < nums.length; i++) {15if (nums[i] > 0) {16res.add(i + 1);17} else {18nums[i] *= -1;19}20}21// [ 5, 6]22return res;23}24}