Skip to content

Commit 64b7dfd

Browse files
committed
Update first missing positive problem
1 parent 227a2a2 commit 64b7dfd

File tree

5 files changed

+26
-24
lines changed

5 files changed

+26
-24
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ List of Programs related to data structures and algorithms
7272
13. Container with most water: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/13.containerWithMostWater/containerWithMostWater.js)
7373
[Playground](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/13.containerWithMostWater/containerWithMostWater.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/13.containerWithMostWater/containerWithMostWater.md)
7474

75-
14. First missing positive number: [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/firstMissingPositive/firstMissingPositive.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/firstMissingPositive/firstMissingPositive.md)
75+
14. First missing positive number: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/14.firstMissingPositive/firstMissingPositive.js) [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/14.firstMissingPositive/firstMissingPositive.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/14.firstMissingPositive/firstMissingPositive.md)
7676

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
**Algorithmic Steps**
22
This problem is solved with the help of in-place input array using its index as hash key. This is known as **in-place hashing** technique. The algorithmic approach can be summarized as follows:
33

4-
1. Initialize a `length` variable to store the length of input array and `nonExistNum` to store out of bounds value.
4+
1. Initialize a `length` variable to store the length of input array and `nonExistNum` variable to `length+1` to indicate out of bounds value in the input array.
55

6-
2. Iterate over the input array and replace their values to `nonExistNum` if the value is not a positive number(i.e, `<=0 or > length`). This logic is helpful to ignore the non-positive values because the smallest positive number exists with in the range between `1` and `length` values. In the worst case, the input array may contain all numbers with in a sequence until the length of array, therefore we will consider `length+1` as the next smallest number.
6+
2. Iterate over the input array and replace their values to `nonExistNum` if the value is not a positive number or out of bounds value(i.e, `<=0 or > length`). This logic is helpful to ignore the non-positive and out of bounds values because the smallest positive number exists with in the range between `1` and `length` values. In the worst case, the input array may contain all numbers with in a sequence until the length of array, therefore we will consider `length+1` as the next smallest number.
77

8-
3. Iterage over the input array again and find the absolute value(i.e, `index`) for each value in the array. If this value is equals to `nonExistNum`, the current iteration is going to be skipped. This absolute value presence will be indicated by negative sign at the correct index position. The correct index position is calculated by `nums[index]`.
8+
3. Iterage over the input array again and find the absolute value(i.e, `index`) for each value in the array. If this value is equals to `nonExistNum`, the current iteration is going to be skipped. This absolute value presence in the sequence array will be indicated by negative sign at the correct index position. The correct index position is calculated by `nums[index]`.
99

10-
**Note:** This negation applies only when the value is positive only.
10+
**Note:** This negation applies only when the value is positive.
1111

1212
4. You need to iterate one final time to find any positive value after the negation process(step 3). If there is any positive value at index `i`, return `i+1` as the first missing positive number.
1313

1414
5. when there are no positive numbers exists, return `length+1` as first missing positive number.
1515

1616

1717
**Time and Space complexity:**
18-
This algorithm has a time complexity of O(n)(i.e, `O(n) + O(n) + O(n)`) because we are traversing array thrice sequentially. Since we are using in-place input array as datastructure, the space complexity will be O(1).
18+
This algorithm has a time complexity of `O(n)`(i.e, `O(n) + O(n) + O(n)`) because we are traversing an array thrice one after the other.
19+
Since we are using in-place input array as datastructure, the space complexity will be `O(1)`.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
**Algorithmic Steps**
2+
This problem is solved with the help of in-place input array using its index as hash key. This is known as **in-place hashing** technique. The algorithmic approach can be summarized as follows:
3+
4+
1. Initialize a `length` variable to store the length of input array and `nonExistNum` variable to `length+1` to indicate out of bounds value in the input array.
5+
6+
2. Iterate over the input array and replace their values to `nonExistNum` if the value is not a positive number or out of bounds value(i.e, `<=0 or > length`). This logic is helpful to ignore the non-positive and out of bounds values because the smallest positive number exists with in the range between `1` and `length` values. In the worst case, the input array may contain all numbers with in a sequence until the length of array, therefore we will consider `length+1` as the next smallest number.
7+
8+
3. Iterage over the input array again and find the absolute value(i.e, `index`) for each value in the array. If this value is equals to `nonExistNum`, the current iteration is going to be skipped. This absolute value presence in the sequence array will be indicated by negative sign at the correct index position. The correct index position is calculated by `nums[index]`.
9+
10+
**Note:** This negation applies only when the value is positive.
11+
12+
4. You need to iterate one final time to find any positive value after the negation process(step 3). If there is any positive value at index `i`, return `i+1` as the first missing positive number.
13+
14+
5. when there are no positive numbers exists, return `length+1` as first missing positive number.
15+
16+
17+
**Time and Space complexity:**
18+
This algorithm has a time complexity of `O(n)`(i.e, `O(n) + O(n) + O(n)`) because we are traversing an array thrice one after the other.
19+
Since we are using in-place input array as datastructure, the space complexity will be `O(1)`.

src/javascript/algorithms/array/firstMissingPositive/firstMissingPositive.md

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)