Skip to content

Commit 0b98585

Browse files
committed
Add two missing numbers problem
1 parent 7bbf0d3 commit 0b98585

File tree

7 files changed

+98
-2
lines changed

7 files changed

+98
-2
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ List of Programs related to data structures and algorithms
7676

7777
15. Best time to buy stock and sell stock: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/15.buySellStock/buySellStock.js) [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/15.buySellStock/buySellStock.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/15.buySellStock/buySellStock.md)
7878

79+
16. Two missing numbers: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/16.twoMissingNumbers/twoMissingNumbers.js) [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/16.twoMissingNumbers/twoMissingNumbers.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/16.twoMissingNumbers/twoMissingNumbers.md)
80+
7981
### String
8082

8183
1. Longest substring without repeating characters: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/longestSubstringWithoutRepeatingChars/longestSubstringWithoutRepeatingChars.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/strings/longestSubstringWithoutRepeatingChars/longestSubstringWithoutRepeatingChars.md)

src/java1/algorithms/array/buySellStock/BuySellStock.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Example 1:
77
Input: prices = [8, 3, 6, 4, 7, 5]
88
Output: 4
99

10+
Example 2:
1011
Input: prices = [7, 6, 5, 4, 3, 2, 1]
1112
Output: 0
1213

@@ -26,5 +27,5 @@ This problem is solved with the help of sliding window approach to calculate the
2627
6. Return `maxProfit` which represents best time(or max proft scenario) to buy and sell stock prices.
2728

2829
**Time and Space complexity:**
29-
This algorithm has a time complexity of `O(n)`, where ``n is the number of stock prices. This is because we are traversing the array at most once.
30+
This algorithm has a time complexity of `O(n)`, where `n` is the number of stock prices. This is because we are traversing the array at most once.
3031
Here, we don't use any additional datastructure other than two pointers. Hence, the space complexity will be `O(1)`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
**Description:**
2+
Given an unsorted set of numbers from `1` to `N` with exactly two of them missing, find those two missing numbers.
3+
4+
### Examples
5+
Example 1:
6+
7+
Input: prices = [3, 2, 5, 1, 6, 8]
8+
Output: [4,7]
9+
10+
Example 2:
11+
12+
Input: prices = [3, 2, 5, 1, 6, 4]
13+
Output: [7,8]
14+
15+
**Algorithmic Steps**
16+
This problem is solved with the help of sequential numbers sum and average calculations. The algorithmic approach can be summarized as follows:
17+
18+
1. Initialize missing two numbers array(i.e, `missingTwoNums`) to an empty array.
19+
20+
2. Store the length of input array along with missing numbers into `len` variable(i.e, `len = nums.length+2`).
21+
22+
3. Calculate the sum of all sequential numbers into `sum` variable.
23+
24+
**Note:** Sum of `n` sequential number sum is `(n *(n+1))/2`.
25+
26+
4. Calculate the sum of given input array into `numsSum` by iterating over the array.
27+
28+
5. The sum of missing two numbers(`missingTwoNumsSum`) is calculated by the difference between sum calculated in step 3 & 4.
29+
30+
6. Find the average of missing two numbers sum into `missingTwoNumsAvg`.
31+
32+
7. Calculate the sequential numbers sum until missing numbers average value and store it in `sumUntilAvg`.
33+
34+
8. Also, calculate the sum of numbers until `missingTwoNumsAvg` into `numsSumUntilAvg` by iterating over the input array.
35+
36+
9. The difference between sums in step7 & 8 gives the first missing number. This number can be pushed to `missingTwoNums` array.
37+
38+
10. The second number is calculated by deducting first number from missing numbers sum. This number also stored into `missingTwoNums` array.
39+
40+
11. Return `missingTwoNums` array which stores two missing numbers.
41+
42+
43+
**Time and Space complexity:**
44+
This algorithm has a time complexity of `O(n)`(`O(n) + O(n)`), where `n` is the number of stock prices. This is because we are traversing the array two times.
45+
46+
Here, we don't use any additional datastructure other than missing numbers array of size 2. Hence, the space complexity will be `O(1)`.

src/javascript/algorithms/array/15.buySellStock/buySellStock.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Example 1:
77
Input: prices = [8, 3, 6, 4, 7, 5]
88
Output: 4
99

10+
Example 2:
1011
Input: prices = [7, 6, 5, 4, 3, 2, 1]
1112
Output: 0
1213

@@ -26,5 +27,5 @@ This problem is solved with the help of sliding window approach to calculate the
2627
6. Return `maxProfit` which represents best time(or max proft scenario) to buy and sell stock prices.
2728

2829
**Time and Space complexity:**
29-
This algorithm has a time complexity of `O(n)`, where ``n is the number of stock prices. This is because we are traversing the array at most once.
30+
This algorithm has a time complexity of `O(n)`, where `n` is the number of stock prices. This is because we are traversing the array at most once.
3031
Here, we don't use any additional datastructure other than two pointers. Hence, the space complexity will be `O(1)`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
**Description:**
2+
Given an unsorted set of numbers from `1` to `N` with exactly two of them missing, find those two missing numbers.
3+
4+
### Examples
5+
Example 1:
6+
7+
Input: prices = [3, 2, 5, 1, 6, 8]
8+
Output: [4,7]
9+
10+
Example 2:
11+
12+
Input: prices = [3, 2, 5, 1, 6, 4]
13+
Output: [7,8]
14+
15+
**Algorithmic Steps**
16+
This problem is solved with the help of sequential numbers sum and average calculations. The algorithmic approach can be summarized as follows:
17+
18+
1. Initialize missing two numbers array(i.e, `missingTwoNums`) to an empty array.
19+
20+
2. Store the length of input array along with missing numbers into `len` variable(i.e, `len = nums.length+2`).
21+
22+
3. Calculate the sum of all sequential numbers into `sum` variable.
23+
24+
**Note:** Sum of `n` sequential number sum is `(n *(n+1))/2`.
25+
26+
4. Calculate the sum of given input array into `numsSum` by iterating over the array.
27+
28+
5. The sum of missing two numbers(`missingTwoNumsSum`) is calculated by the difference between sum calculated in step 3 & 4.
29+
30+
6. Find the average of missing two numbers sum into `missingTwoNumsAvg`.
31+
32+
7. Calculate the sequential numbers sum until missing numbers average value and store it in `sumUntilAvg`.
33+
34+
8. Also, calculate the sum of numbers until `missingTwoNumsAvg` into `numsSumUntilAvg` by iterating over the input array.
35+
36+
9. The difference between sums in step7 & 8 gives the first missing number. This number can be pushed to `missingTwoNums` array.
37+
38+
10. The second number is calculated by deducting first number from missing numbers sum. This number also stored into `missingTwoNums` array.
39+
40+
11. Return `missingTwoNums` array which stores two missing numbers.
41+
42+
43+
**Time and Space complexity:**
44+
This algorithm has a time complexity of `O(n)`(`O(n) + O(n)`), where `n` is the number of stock prices. This is because we are traversing the array two times.
45+
46+
Here, we don't use any additional datastructure other than missing numbers array of size 2. Hence, the space complexity will be `O(1)`.

src/javascript/algorithms/array/twoMissingNumbers/twoMissingNumbers.md

Whitespace-only changes.

0 commit comments

Comments
 (0)