patternjavaMinor
Finding the smallest missing positive number in an array
Viewed 0 times
smallestthenumberarrayfindingmissingpositive
Problem
Any suggestions to improve?
public class FindSmallestMissingPositive {
public static void main(String args[]){
int a[] = {3,5,4,-1,1,-1,0};
System.out.println("Smallest Missing positive : "+ findSmPositive(a));
}
public static int findSmPositive(int input[]){
int min = input[0];
int max= input[0];
HashSet set = new HashSet();
for(int i=0;imax){
max = input[i];
}
}
for(int i=min;i0 && !set.contains(i)){
return i;
}
else continue;
}
return -1;
}
}Solution
Bugs
The implementation incorrectly gives -1 for all these input:
Improving the algorithm
The algorithm you used is \$O(n)\$ time and \$O(n)\$ space.
If you don't mind modifying the input array,
then you could reuse it as the storage,
making the algorithm \$O(1)\$ space.
Consider this alternative algorithm:
The logic behind this algorithm:
Strange coding style
Although these type declarations compile, they are not recommended:
The recommended writing style:
Also,
the code formatting style is not great, at many places it's too compact.
I suggest to copy paste into an IDE like IntelliJ or Eclipse and use the auto-formatting function to reformat nicely.
Testing your improvements
The problem description seems to match this one on leetcode.
After you implement your improvements,
I suggest posting there to verify the correctness of your implementation.
https://leetcode.com/problems/first-missing-positive/
The implementation incorrectly gives -1 for all these input:
[0]should return 1
[1]should return 2
[2]should return 1
[0,2,2,1,1]should return 3
[1,2,2,1,3,1,0,4,0]should return 5
Improving the algorithm
The algorithm you used is \$O(n)\$ time and \$O(n)\$ space.
If you don't mind modifying the input array,
then you could reuse it as the storage,
making the algorithm \$O(1)\$ space.
Consider this alternative algorithm:
- If the array is empty, return 1
- Loop over the array indexes
- While
input[i] - 1is a valid array index and not in the correct position, andinput[input[i] - 1]is not in the correct position, then swap the elementsiandinput[i] - 1.
- Loop over the array indexes again, and when you find a value not in the correct position, then return
i + 1.
- Return the last value + 1.
The logic behind this algorithm:
- Swap elements into the correct position to get a sequence of positive integers 1, 2, 3, ...
- Ignore values that cannot be in this array (
- The beginning of the array will be filled with correct sequence values, gaps will be filled with junk, and the second looping pass will short-circuit on the first incorrect value
Strange coding style
Although these type declarations compile, they are not recommended:
public static void main(String args[]) {
public static int findSmPositive(int input[]) {The recommended writing style:
public static void main(String[] args) {
public static int findSmPositive(int[] input) {Also,
the code formatting style is not great, at many places it's too compact.
I suggest to copy paste into an IDE like IntelliJ or Eclipse and use the auto-formatting function to reformat nicely.
Testing your improvements
The problem description seems to match this one on leetcode.
After you implement your improvements,
I suggest posting there to verify the correctness of your implementation.
https://leetcode.com/problems/first-missing-positive/
Code Snippets
public static void main(String args[]) {
public static int findSmPositive(int input[]) {public static void main(String[] args) {
public static int findSmPositive(int[] input) {Context
StackExchange Code Review Q#139801, answer score: 6
Revisions (0)
No revisions yet.