Skip to content

Commit 41abcba

Browse files
committed
Add missing numbers problem
1 parent a338499 commit 41abcba

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package java1.algorithms.array.disappearedNumbers;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class DisappearedNumbers {
7+
8+
private static List<Integer> findDisappearedNumbers(int[] nums){
9+
for (int i = 0; i < nums.length; i++) {
10+
int index = Math.abs(nums[i])-1;
11+
if(nums[index]>0) {
12+
nums[index] = -nums[index];
13+
}
14+
}
15+
16+
List<Integer> missingNumbers = new ArrayList<>();
17+
for (int i = 0; i < nums.length; i++) {
18+
if(nums[i] >0) {
19+
missingNumbers.add(i+1);
20+
}
21+
}
22+
23+
return missingNumbers;
24+
}
25+
public static void main(String[] args) {
26+
int[] nums1= new int[]{6, 3, 1, 5, 3, 7, 5};
27+
int[] nums2= new int[]{1, 1};
28+
29+
System.out.println(findDisappearedNumbers(nums1));
30+
System.out.println(findDisappearedNumbers(nums2));
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
**Description:**
2+
Given an integer array `nums` of length `n` where `nums[i]` is in the range `[1, n]`, find an array of all the integers in the range(`[1,n]`) that do not appear in given `nums` array.
3+
4+
**Note:** The solution needs to be done without an extra space.
5+
6+
### Examples
7+
Example 1:
8+
9+
Input: nums = [6, 3, 1, 5, 3, 7, 5]
10+
Output: [2,4]
11+
12+
Example 2:
13+
14+
Input: nums = [1,1],
15+
Output: [2]
16+
17+
**Algorithmic Steps**
18+
This problem is solved with the help of array traversal and marking the available numbers. The algorithmic approach can be summarized as follows:
19+
20+
1. Create a function named `findDisappearedNumbers`, which accepts input array(`nums`) to find the disppeared numbers.
21+
22+
2. Iterate over given array(`num`), fetch the respective index(0 based) for absolute current value. If the number at the index position is positive, mark the number by negating it.
23+
24+
3. Define an empty missing numbers array(`missingNumbers`).
25+
26+
4. Iterate over the input array again, add the next index position as missing number(`i+1`) to missing numbers array.
27+
28+
**Time and Space complexity:**
29+
This algorithm has a time complexity of `O(n)`, where `n` is the number of elements in an array. This is because we need to iterate over all the elements at most twice for finding the missing numbers.
30+
31+
It takes constant time complexity of `O(1)` due to no additional datastructure been used other than output array(not considered as extra memory).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function findDisappearedNumbers(nums){
2+
for (let i = 0; i < nums.length; i++) {
3+
const index = Math.abs(nums[i])-1;
4+
if(nums[index]>0) {
5+
nums[index] = -nums[index];
6+
}
7+
}
8+
9+
let missingNumbers = [];
10+
for (let i = 0; i < nums.length; i++) {
11+
if(nums[i] >0) {
12+
missingNumbers.push(i+1);
13+
}
14+
}
15+
16+
return missingNumbers;
17+
}
18+
19+
const nums1= [6, 3, 1, 5, 3, 7, 5];
20+
const nums2= [1,1];
21+
22+
console.log(findDisappearedNumbers(nums1));
23+
console.log(findDisappearedNumbers(nums2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
**Description:**
2+
Given an integer array `nums` of length `n` where `nums[i]` is in the range `[1, n]`, find an array of all the integers in the range(`[1,n]`) that do not appear in given `nums` array.
3+
4+
**Note:** The solution needs to be done without an extra space.
5+
6+
### Examples
7+
Example 1:
8+
9+
Input: nums = [6, 3, 1, 5, 3, 7, 5]
10+
Output: [2,4]
11+
12+
Example 2:
13+
14+
Input: nums = [1,1],
15+
Output: [2]
16+
17+
**Algorithmic Steps**
18+
This problem is solved with the help of array traversal and marking the available numbers. The algorithmic approach can be summarized as follows:
19+
20+
1. Create a function named `findDisappearedNumbers`, which accepts input array(`nums`) to find the disppeared numbers.
21+
22+
2. Iterate over given array(`num`), fetch the respective index(0 based) for absolute current value. If the number at the index position is positive, mark the number by negating it.
23+
24+
3. Define an empty missing numbers array(`missingNumbers`).
25+
26+
4. Iterate over the input array again, add the next index position as missing number(`i+1`) to missing numbers array.
27+
28+
**Time and Space complexity:**
29+
This algorithm has a time complexity of `O(n)`, where `n` is the number of elements in an array. This is because we need to iterate over all the elements at most twice for finding the missing numbers.
30+
31+
It takes constant time complexity of `O(1)` due to no additional datastructure been used other than output array(not considered as extra memory).

0 commit comments

Comments
 (0)