Skip to content

Commit 6445d09

Browse files
Merge branch 'master' into BAEL-3335
2 parents f0742d2 + 1acf4e6 commit 6445d09

File tree

1,062 files changed

+13850
-8522
lines changed

Some content is hidden

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

1,062 files changed

+13850
-8522
lines changed

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,10 @@ software-security/sql-injection-samples/derby.log
8484
spring-soap/src/main/java/com/baeldung/springsoap/gen/
8585
/report-*.json
8686
transaction.log
87-
*-shell.log
87+
*-shell.log
88+
89+
apache-cxf/cxf-aegis/baeldung.xml
90+
apache-fop/src/test/resources/input.xml
91+
apache-fop/src/test/resources/output_herold.pdf
92+
apache-fop/src/test/resources/output_html2fo.pdf
93+
apache-fop/src/test/resources/output_jtidy.pdf
817 Bytes
Loading

algorithms-miscellaneous-3/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ This module contains articles about algorithms. Some classes of algorithms, e.g.
1616
- [Efficient Word Frequency Calculator in Java](https://www.baeldung.com/java-word-frequency)
1717
- [Interpolation Search in Java](https://www.baeldung.com/java-interpolation-search)
1818
- [The K-Means Clustering Algorithm in Java](https://www.baeldung.com/java-k-means-clustering-algorithm)
19+
- [Creating a Custom Annotation in Java](https://www.baeldung.com/java-custom-annotation)
20+
- [Breadth-First Search Algorithm in Java](https://www.baeldung.com/java-breadth-first-search)
1921
- More articles: [[<-- prev]](/algorithms-miscellaneous-2) [[next -->]](/algorithms-miscellaneous-4)

algorithms-miscellaneous-4/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
## Algorithms - Miscellaneous
22

3-
This module contains articles about algorithms. Some classes of algorithms, e.g., [sorting](/../algorithms-sorting) and
4-
[genetic algorithms](/../algorithms-genetic), have their own dedicated modules.
3+
This module contains articles about algorithms. Some classes of algorithms, e.g., [sorting](https://github.com/eugenp/tutorials/blob/algorithms-sorting) and [genetic algorithms](https://github.com/eugenp/tutorials/blob/algorithms-genetic), have their own dedicated modules.
54

65
### Relevant articles:
76

@@ -12,4 +11,4 @@ This module contains articles about algorithms. Some classes of algorithms, e.g.
1211
- [Find Substrings That Are Palindromes in Java](https://www.baeldung.com/java-palindrome-substrings)
1312
- [Find the Longest Substring without Repeating Characters](https://www.baeldung.com/java-longest-substring-without-repeated-characters)
1413
- [Permutations of an Array in Java](https://www.baeldung.com/java-array-permutations)
15-
- More articles: [[<-- prev]](/../algorithms-miscellaneous-3) [[next -->]](/../algorithms-miscellaneous-5)
14+
- More articles: [[<-- prev]](/algorithms-miscellaneous-3) [[next -->]](/algorithms-miscellaneous-5)
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+
}

algorithms-miscellaneous-5/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ This module contains articles about algorithms. Some classes of algorithms, e.g.
88
- [Converting Between Byte Arrays and Hexadecimal Strings in Java](https://www.baeldung.com/java-byte-arrays-hex-strings)
99
- [Reversing a Binary Tree in Java](https://www.baeldung.com/java-reversing-a-binary-tree)
1010
- [Find If Two Numbers Are Relatively Prime in Java](https://www.baeldung.com/java-two-relatively-prime-numbers)
11+
- [Knapsack Problem Implementation in Java](https://www.baeldung.com/java-knapsack)
1112
- More articles: [[<-- prev]](/../algorithms-miscellaneous-4)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.algorithms.balancedbinarytree;
2+
3+
public class BalancedBinaryTree {
4+
5+
public static boolean isBalanced(Tree tree) {
6+
return isBalancedRecursive(tree, -1).isBalanced;
7+
}
8+
9+
private static Result isBalancedRecursive(Tree tree, int depth) {
10+
if (tree == null) {
11+
return new Result(true, -1);
12+
}
13+
14+
Result leftSubtreeResult = isBalancedRecursive(tree.left(), depth + 1);
15+
Result rightSubtreeResult = isBalancedRecursive(tree.right(), depth + 1);
16+
17+
boolean isBalanced = Math.abs(leftSubtreeResult.height - rightSubtreeResult.height) <= 1;
18+
boolean subtreesAreBalanced = leftSubtreeResult.isBalanced && rightSubtreeResult.isBalanced;
19+
int height = Math.max(leftSubtreeResult.height, rightSubtreeResult.height) + 1;
20+
21+
return new Result(isBalanced && subtreesAreBalanced, height);
22+
}
23+
24+
private static final class Result {
25+
private final boolean isBalanced;
26+
private final int height;
27+
28+
private Result(boolean isBalanced, int height) {
29+
this.isBalanced = isBalanced;
30+
this.height = height;
31+
}
32+
}
33+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.algorithms.balancedbinarytree;
2+
3+
public class Tree {
4+
private final int value;
5+
private final Tree left;
6+
private final Tree right;
7+
8+
public Tree(int value, Tree left, Tree right) {
9+
this.value = value;
10+
this.left = left;
11+
this.right = right;
12+
}
13+
14+
public int value() {
15+
return value;
16+
}
17+
18+
public Tree left() {
19+
return left;
20+
}
21+
22+
public Tree right() {
23+
return right;
24+
}
25+
26+
@Override
27+
public String toString() {
28+
return String.format("[%s, %s, %s]",
29+
value,
30+
left == null ? "null" : left.toString(),
31+
right == null ? "null" : right.toString()
32+
);
33+
}
34+
}
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+
}

0 commit comments

Comments
 (0)