Skip to content

Commit 6838269

Browse files
authored
Merge branch 'master' into master
2 parents a2dbb06 + bfced1b commit 6838269

File tree

343 files changed

+3936
-1200
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

343 files changed

+3936
-1200
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.algorithms.smallestinteger;
2+
3+
import java.util.Arrays;
4+
5+
public class SmallestMissingPositiveInteger {
6+
public static int searchInSortedArray(int[] input) {
7+
for (int i = 0; i < input.length; i++) {
8+
if (i != input[i]) {
9+
return i;
10+
}
11+
}
12+
13+
return input.length;
14+
}
15+
16+
public static int searchInUnsortedArraySortingFirst(int[] input) {
17+
Arrays.sort(input);
18+
return searchInSortedArray(input);
19+
}
20+
21+
public static int searchInUnsortedArrayBooleanArray(int[] input) {
22+
boolean[] flags = new boolean[input.length];
23+
for (int number : input) {
24+
if (number < flags.length) {
25+
flags[number] = true;
26+
}
27+
}
28+
29+
for (int i = 0; i < flags.length; i++) {
30+
if (!flags[i]) {
31+
return i;
32+
}
33+
}
34+
35+
return flags.length;
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.baeldung.algorithms.smallestinteger;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
7+
class SmallestMissingPositiveIntegerUnitTest {
8+
@Test
9+
void givenArrayWithThreeMissing_whenSearchInSortedArray_thenThree() {
10+
int[] input = new int[] {0, 1, 2, 4, 5};
11+
12+
int result = SmallestMissingPositiveInteger.searchInSortedArray(input);
13+
14+
assertThat(result).isEqualTo(3);
15+
}
16+
17+
@Test
18+
void givenArrayWithOneAndThreeMissing_whenSearchInSortedArray_thenOne() {
19+
int[] input = new int[] {0, 2, 4, 5};
20+
21+
int result = SmallestMissingPositiveInteger.searchInSortedArray(input);
22+
23+
assertThat(result).isEqualTo(1);
24+
}
25+
26+
@Test
27+
void givenArrayWithoutMissingInteger_whenSearchInSortedArray_thenArrayLength() {
28+
int[] input = new int[] {0, 1, 2, 3, 4, 5};
29+
30+
int result = SmallestMissingPositiveInteger.searchInSortedArray(input);
31+
32+
assertThat(result).isEqualTo(input.length);
33+
}
34+
35+
@Test
36+
void givenArrayWithThreeMissing_whenSearchInUnsortedArraySortingFirst_thenThree() {
37+
int[] input = new int[] {1, 4, 0, 5, 2};
38+
39+
int result = SmallestMissingPositiveInteger.searchInUnsortedArraySortingFirst(input);
40+
41+
assertThat(result).isEqualTo(3);
42+
}
43+
44+
@Test
45+
void givenArrayWithOneAndThreeMissing_whenSearchInUnsortedArraySortingFirst_thenOne() {
46+
int[] input = new int[] {4, 2, 0, 5};
47+
48+
int result = SmallestMissingPositiveInteger.searchInUnsortedArraySortingFirst(input);
49+
50+
assertThat(result).isEqualTo(1);
51+
}
52+
53+
@Test
54+
void givenArrayWithoutMissingInteger_whenSearchInUnsortedArraySortingFirst_thenArrayLength() {
55+
int[] input = new int[] {4, 5, 1, 3, 0, 2};
56+
57+
int result = SmallestMissingPositiveInteger.searchInUnsortedArraySortingFirst(input);
58+
59+
assertThat(result).isEqualTo(input.length);
60+
}
61+
62+
@Test
63+
void givenArrayWithThreeMissing_whenSearchInUnsortedArrayBooleanArray_thenThree() {
64+
int[] input = new int[] {1, 4, 0, 5, 2};
65+
66+
int result = SmallestMissingPositiveInteger.searchInUnsortedArrayBooleanArray(input);
67+
68+
assertThat(result).isEqualTo(3);
69+
}
70+
71+
@Test
72+
void givenArrayWithOneAndThreeMissing_whenSearchInUnsortedArrayBooleanArray_thenOne() {
73+
int[] input = new int[] {4, 2, 0, 5};
74+
75+
int result = SmallestMissingPositiveInteger.searchInUnsortedArrayBooleanArray(input);
76+
77+
assertThat(result).isEqualTo(1);
78+
}
79+
80+
@Test
81+
void givenArrayWithoutMissingInteger_whenSearchInUnsortedArrayBooleanArray_thenArrayLength() {
82+
int[] input = new int[] {4, 5, 1, 3, 0, 2};
83+
84+
int result = SmallestMissingPositiveInteger.searchInUnsortedArrayBooleanArray(input);
85+
86+
assertThat(result).isEqualTo(input.length);
87+
}
88+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.algorithms.binarygap;
2+
3+
public class BinaryGap {
4+
static int calculateBinaryGap(int n) {
5+
return calculateBinaryGap(n >>> Integer.numberOfTrailingZeros(n), 0, 0);
6+
}
7+
8+
static int calculateBinaryGap(int n, int current, int maximum) {
9+
if (n == 0) {
10+
return maximum;
11+
} else if ((n & 1) == 0) {
12+
return calculateBinaryGap(n >>> 1, current + 1, maximum);
13+
} else {
14+
return calculateBinaryGap(n >>> 1, 0, Math.max(maximum, current));
15+
}
16+
}
17+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.algorithms.binarygap;
2+
3+
import static com.baeldung.algorithms.binarygap.BinaryGap.calculateBinaryGap;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
public class BinaryGapUnitTest {
9+
10+
@Test public void givenNoOccurrenceOfBoundedZeros_whenCalculateBinaryGap_thenOutputCorrectResult() {
11+
12+
int result = calculateBinaryGap(63);
13+
assertEquals(0, result);
14+
}
15+
16+
@Test public void givenTrailingZeros_whenCalculateBinaryGap_thenOutputCorrectResult() {
17+
18+
int result = calculateBinaryGap(40);
19+
assertEquals(1, result);
20+
}
21+
22+
@Test public void givenSingleOccurrenceOfBoundedZeros_whenCalculateBinaryGap_thenOutputCorrectResult() {
23+
24+
int result = calculateBinaryGap(9);
25+
assertEquals(2, result);
26+
}
27+
28+
@Test public void givenMultipleOccurrenceOfBoundedZeros_whenCalculateBinaryGap_thenOutputCorrectResult() {
29+
30+
int result = calculateBinaryGap(145);
31+
assertEquals(3, result);
32+
}
33+
34+
}

core-java-modules/README.md

Lines changed: 0 additions & 1 deletion
Lines changed: 17 additions & 0 deletions
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<artifactId>core-java-9</artifactId>
5+
<version>0.2-SNAPSHOT</version>
6+
<name>core-java-9</name>
7+
8+
<parent>
9+
<groupId>com.baeldung</groupId>
10+
<artifactId>parent-modules</artifactId>
11+
<version>1.0.0-SNAPSHOT</version>
12+
<relativePath>../../</relativePath>
13+
</parent>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>com.jayway.awaitility</groupId>
18+
<artifactId>awaitility</artifactId>
19+
<version>${awaitility.version}</version>
20+
<scope>test</scope>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.assertj</groupId>
24+
<artifactId>assertj-core</artifactId>
25+
<version>${assertj.version}</version>
26+
<scope>test</scope>
27+
</dependency>
28+
<dependency>
29+
<groupId>com.google.guava</groupId>
30+
<artifactId>guava</artifactId>
31+
<version>${guava.version}</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.junit.platform</groupId>
35+
<artifactId>junit-platform-runner</artifactId>
36+
<version>${junit.platform.version}</version>
37+
<scope>test</scope>
38+
</dependency>
39+
</dependencies>
40+
41+
<build>
42+
<finalName>core-java-9</finalName>
43+
<plugins>
44+
<plugin>
45+
<groupId>org.apache.maven.plugins</groupId>
46+
<artifactId>maven-compiler-plugin</artifactId>
47+
<version>${maven-compiler-plugin.version}</version>
48+
<configuration>
49+
<source>${maven.compiler.source}</source>
50+
<target>${maven.compiler.target}</target>
51+
</configuration>
52+
</plugin>
53+
</plugins>
54+
</build>
55+
56+
<pluginRepositories>
57+
<pluginRepository>
58+
<id>apache.snapshots</id>
59+
<url>http://repository.apache.org/snapshots/</url>
60+
</pluginRepository>
61+
</pluginRepositories>
62+
63+
<properties>
64+
<!-- testing -->
65+
<assertj.version>3.10.0</assertj.version>
66+
<junit.platform.version>1.2.0</junit.platform.version>
67+
<awaitility.version>1.7.0</awaitility.version>
68+
<maven.compiler.source>1.9</maven.compiler.source>
69+
<maven.compiler.target>1.9</maven.compiler.target>
70+
<guava.version>25.1-jre</guava.version>
71+
</properties>
72+
73+
</project>

0 commit comments

Comments
 (0)