Algorithm
Problem Name: 448. Find All Numbers Disappeared in an Array
Given an array nums
of n
integers where nums[i]
is in the range [1, n]
, return an array of all the integers in the range [1, n]
that do not appear in nums
.
Example 1:
Input: nums = [4,3,2,7,8,2,3,1] Output: [5,6]
Example 2:
Input: nums = [1,1] Output: [2]
Constraints:
n == nums.length
1 <= n <= 105
1 <= nums[i] <= n
Code Examples
#1 Code Example with C Programming
Code -
C Programming
int* findDisappearedNumbers(int* nums, int numsSize, int* returnSize) {
int *p;
int i, j, x;
p = malloc(numsSize * sizeof(int));
//assert(p);
for (i = 0; i < numsSize; i ++) {
x = nums[i];
x = (x > 0) ? x - 1 : 0 - x - 1;
if (nums[x] > 0) nums[x] = 0 - nums[x];
}
j = 0;
for (i = 1; i < = numsSize; i ++) {
if (nums[i - 1] > 0) {
p[j ++] = i;
}
}
*returnSize = j;
return p;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Java Programming
Code -
Java Programming
class Solution {
public List findDisappearedNumbers(int[] nums) {
List result = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
int idx = Math.abs(nums[i]) - 1;
if (nums[idx] > 0) {
nums[idx] *= -1;
}
}
for (int i = 0; i < nums.length; i++) {
if (nums[i] > 0) {
result.add(i + 1);
}
}
return result;
}
}
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Javascript Programming
Code -
Javascript Programming
const findDisappearedNumbers = function(nums) {
const res = [];
nums.forEach((el, idx) => {
res[el - 1] = 1;
});
const arr = [];
for (let i = 0; i < nums.length; i++) {
if (res[i] == null) {
arr.push(i + 1);
}
}
return arr;
};
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with Python Programming
Code -
Python Programming
class Solution:
def findDisappearedNumbers(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
return [x for x in set([i for i in range(1,len(nums)+1)])-set(nums)]
Copy The Code &
Try With Live Editor
Input
Output
#5 Code Example with C# Programming
Code -
C# Programming
using System;
using System.Collections.Generic;
namespace LeetCode
{
public class _0448_FindAllNumbersDisappearedInAnArray
{
public IList < int> FindDisappearedNumbers(int[] nums)
{
for (int i = 0; i < nums.Length; i++)
{
var index = Math.Abs(nums[i]) - 1;
if (nums[index] > 0)
nums[index] *= -1;
}
var result = new List < int>();
for (int i = 0; i < nums.Length; i++)
if (nums[i] > 0)
result.Add(i + 1);
return result;
}
}
}
Copy The Code &
Try With Live Editor
Input
Output