Merge pull request #8084 from dupirefr/bael-3348
dupirefr/dupire.francois+pro@gmail.com [BAEL-3348] Smallest missing positive integer in an array
This commit is contained in:
commit
289c3e4c34
@ -0,0 +1,37 @@
|
||||
package com.baeldung.algorithms.smallestinteger;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class SmallestMissingPositiveInteger {
|
||||
public static int searchInSortedArray(int[] input) {
|
||||
for (int i = 0; i < input.length; i++) {
|
||||
if (i != input[i]) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return input.length;
|
||||
}
|
||||
|
||||
public static int searchInUnsortedArraySortingFirst(int[] input) {
|
||||
Arrays.sort(input);
|
||||
return searchInSortedArray(input);
|
||||
}
|
||||
|
||||
public static int searchInUnsortedArrayBooleanArray(int[] input) {
|
||||
boolean[] flags = new boolean[input.length];
|
||||
for (int number : input) {
|
||||
if (number < flags.length) {
|
||||
flags[number] = true;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < flags.length; i++) {
|
||||
if (!flags[i]) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return flags.length;
|
||||
}
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
package com.baeldung.algorithms.smallestinteger;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class SmallestMissingPositiveIntegerUnitTest {
|
||||
@Test
|
||||
void givenArrayWithThreeMissing_whenSearchInSortedArray_thenThree() {
|
||||
int[] input = new int[] {0, 1, 2, 4, 5};
|
||||
|
||||
int result = SmallestMissingPositiveInteger.searchInSortedArray(input);
|
||||
|
||||
assertThat(result).isEqualTo(3);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenArrayWithOneAndThreeMissing_whenSearchInSortedArray_thenOne() {
|
||||
int[] input = new int[] {0, 2, 4, 5};
|
||||
|
||||
int result = SmallestMissingPositiveInteger.searchInSortedArray(input);
|
||||
|
||||
assertThat(result).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenArrayWithoutMissingInteger_whenSearchInSortedArray_thenArrayLength() {
|
||||
int[] input = new int[] {0, 1, 2, 3, 4, 5};
|
||||
|
||||
int result = SmallestMissingPositiveInteger.searchInSortedArray(input);
|
||||
|
||||
assertThat(result).isEqualTo(input.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenArrayWithThreeMissing_whenSearchInUnsortedArraySortingFirst_thenThree() {
|
||||
int[] input = new int[] {1, 4, 0, 5, 2};
|
||||
|
||||
int result = SmallestMissingPositiveInteger.searchInUnsortedArraySortingFirst(input);
|
||||
|
||||
assertThat(result).isEqualTo(3);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenArrayWithOneAndThreeMissing_whenSearchInUnsortedArraySortingFirst_thenOne() {
|
||||
int[] input = new int[] {4, 2, 0, 5};
|
||||
|
||||
int result = SmallestMissingPositiveInteger.searchInUnsortedArraySortingFirst(input);
|
||||
|
||||
assertThat(result).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenArrayWithoutMissingInteger_whenSearchInUnsortedArraySortingFirst_thenArrayLength() {
|
||||
int[] input = new int[] {4, 5, 1, 3, 0, 2};
|
||||
|
||||
int result = SmallestMissingPositiveInteger.searchInUnsortedArraySortingFirst(input);
|
||||
|
||||
assertThat(result).isEqualTo(input.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenArrayWithThreeMissing_whenSearchInUnsortedArrayBooleanArray_thenThree() {
|
||||
int[] input = new int[] {1, 4, 0, 5, 2};
|
||||
|
||||
int result = SmallestMissingPositiveInteger.searchInUnsortedArrayBooleanArray(input);
|
||||
|
||||
assertThat(result).isEqualTo(3);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenArrayWithOneAndThreeMissing_whenSearchInUnsortedArrayBooleanArray_thenOne() {
|
||||
int[] input = new int[] {4, 2, 0, 5};
|
||||
|
||||
int result = SmallestMissingPositiveInteger.searchInUnsortedArrayBooleanArray(input);
|
||||
|
||||
assertThat(result).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenArrayWithoutMissingInteger_whenSearchInUnsortedArrayBooleanArray_thenArrayLength() {
|
||||
int[] input = new int[] {4, 5, 1, 3, 0, 2};
|
||||
|
||||
int result = SmallestMissingPositiveInteger.searchInUnsortedArrayBooleanArray(input);
|
||||
|
||||
assertThat(result).isEqualTo(input.length);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user