Skip to content

Commit 162b19a

Browse files
Merge branch 'master' into feature-7443-jdk-compatibility-matrix
2 parents 415043f + 51137a1 commit 162b19a

25 files changed

Lines changed: 400 additions & 166 deletions

src/main/java/com/thealgorithms/bitmanipulation/BinaryPalindromeCheck.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
* </p>
1010
*
1111
* @author Hardvan
12+
* @see com.thealgorithms.strings.Palindrome
13+
* @see com.thealgorithms.stacks.PalindromeWithStack
14+
* @see com.thealgorithms.maths.LowestBasePalindrome
15+
* @see com.thealgorithms.datastructures.lists.PalindromeSinglyLinkedList
16+
* @see com.thealgorithms.maths.PalindromePrime
17+
* @see com.thealgorithms.maths.PalindromeNumber
1218
*/
1319
public final class BinaryPalindromeCheck {
1420
private BinaryPalindromeCheck() {

src/main/java/com/thealgorithms/conversions/Base64.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,15 @@ public static byte[] decode(String input) {
119119

120120
// Validate padding: '=' can only appear at the end (last 1 or 2 chars)
121121
int firstPadding = input.indexOf('=');
122-
if (firstPadding != -1 && firstPadding < input.length() - 2) {
123-
throw new IllegalArgumentException("Padding '=' can only appear at the end (last 1 or 2 characters)");
122+
if (firstPadding != -1) {
123+
if (firstPadding < input.length() - 2) {
124+
throw new IllegalArgumentException("Padding '=' can only appear at the end (last 1 or 2 characters)");
125+
}
126+
for (int i = firstPadding; i < input.length(); i++) {
127+
if (input.charAt(i) != '=') {
128+
throw new IllegalArgumentException("A padding '=' must not be followed by a non-padding character");
129+
}
130+
}
124131
}
125132

126133
List<Byte> result = new ArrayList<>();

src/main/java/com/thealgorithms/datastructures/graphs/DijkstraAlgorithm.java

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.datastructures.graphs;
22

33
import java.util.Arrays;
4+
import java.util.PriorityQueue;
45

56
/**
67
* Dijkstra's algorithm for finding the shortest path from a single source vertex to all other vertices in a graph.
@@ -18,6 +19,21 @@ public DijkstraAlgorithm(int vertexCount) {
1819
this.vertexCount = vertexCount;
1920
}
2021

22+
private static class Node implements Comparable<Node> {
23+
int id;
24+
int distance;
25+
26+
Node(int id, int distance) {
27+
this.id = id;
28+
this.distance = distance;
29+
}
30+
31+
@Override
32+
public int compareTo(Node other) {
33+
return Integer.compare(this.distance, other.distance);
34+
}
35+
}
36+
2137
/**
2238
* Executes Dijkstra's algorithm on the provided graph to find the shortest paths from the source vertex to all other vertices.
2339
*
@@ -36,18 +52,25 @@ public int[] run(int[][] graph, int source) {
3652

3753
int[] distances = new int[vertexCount];
3854
boolean[] processed = new boolean[vertexCount];
55+
PriorityQueue<Node> unprocessed = new PriorityQueue<>();
3956

4057
Arrays.fill(distances, Integer.MAX_VALUE);
41-
Arrays.fill(processed, false);
4258
distances[source] = 0;
59+
unprocessed.add(new Node(source, 0));
60+
61+
while (!unprocessed.isEmpty()) {
62+
Node current = unprocessed.poll();
63+
int u = current.id;
4364

44-
for (int count = 0; count < vertexCount - 1; count++) {
45-
int u = getMinDistanceVertex(distances, processed);
65+
if (processed[u]) {
66+
continue;
67+
}
4668
processed[u] = true;
4769

4870
for (int v = 0; v < vertexCount; v++) {
4971
if (!processed[v] && graph[u][v] != 0 && distances[u] != Integer.MAX_VALUE && distances[u] + graph[u][v] < distances[v]) {
5072
distances[v] = distances[u] + graph[u][v];
73+
unprocessed.add(new Node(v, distances[v]));
5174
}
5275
}
5376
}
@@ -56,27 +79,6 @@ public int[] run(int[][] graph, int source) {
5679
return distances;
5780
}
5881

59-
/**
60-
* Finds the vertex with the minimum distance value from the set of vertices that have not yet been processed.
61-
*
62-
* @param distances The array of current shortest distances from the source vertex.
63-
* @param processed The array indicating whether each vertex has been processed.
64-
* @return The index of the vertex with the minimum distance value.
65-
*/
66-
private int getMinDistanceVertex(int[] distances, boolean[] processed) {
67-
int min = Integer.MAX_VALUE;
68-
int minIndex = -1;
69-
70-
for (int v = 0; v < vertexCount; v++) {
71-
if (!processed[v] && distances[v] <= min) {
72-
min = distances[v];
73-
minIndex = v;
74-
}
75-
}
76-
77-
return minIndex;
78-
}
79-
8082
/**
8183
* Prints the shortest distances from the source vertex to all other vertices.
8284
*

src/main/java/com/thealgorithms/datastructures/graphs/DijkstraOptimizedAlgorithm.java

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

src/main/java/com/thealgorithms/misc/PalindromeSinglyLinkedList.java renamed to src/main/java/com/thealgorithms/datastructures/lists/PalindromeSinglyLinkedList.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.misc;
1+
package com.thealgorithms.datastructures.lists;
22

33
import java.util.Stack;
44

@@ -9,6 +9,13 @@
99
*
1010
* See more:
1111
* https://www.geeksforgeeks.org/function-to-check-if-a-singly-linked-list-is-palindrome/
12+
*
13+
* @see com.thealgorithms.strings.Palindrome
14+
* @see com.thealgorithms.stacks.PalindromeWithStack
15+
* @see com.thealgorithms.bitmanipulation.BinaryPalindromeCheck
16+
* @see com.thealgorithms.maths.LowestBasePalindrome
17+
* @see com.thealgorithms.maths.PalindromePrime
18+
* @see com.thealgorithms.maths.PalindromeNumber
1219
*/
1320
@SuppressWarnings("rawtypes")
1421
public final class PalindromeSinglyLinkedList {

src/main/java/com/thealgorithms/tree/HeavyLightDecomposition.java renamed to src/main/java/com/thealgorithms/datastructures/trees/HeavyLightDecomposition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.tree;
1+
package com.thealgorithms.datastructures.trees;
22

33
import java.util.ArrayList;
44
import java.util.List;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.thealgorithms.maths;
2+
3+
/**
4+
* Two numbers are Friendly if they share the same abundancy index,
5+
* which is the ratio of the sum of divisors to the number itself.
6+
* Example: 6 and 28 are friendly because sigma(6)/6 = 2 and sigma(28)/28 = 2
7+
*
8+
* @see <a href="https://en.wikipedia.org/wiki/Friendly_number">
9+
* Wikipedia: Friendly Number</a>
10+
*
11+
* @author Vraj Prajapati @Rosander0
12+
*/
13+
public final class FriendlyNumber {
14+
15+
private FriendlyNumber() {
16+
// Utility class
17+
}
18+
19+
private static int sumOfDivisors(final int number) {
20+
int sum = 0;
21+
final int root = (int) Math.sqrt(number);
22+
for (int i = 1; i <= root; i++) {
23+
if (number % i == 0) {
24+
sum += i;
25+
final int other = number / i;
26+
if (other != i) {
27+
sum += other;
28+
}
29+
}
30+
}
31+
return sum;
32+
}
33+
34+
/**
35+
* Checks whether two numbers are Friendly Numbers.
36+
*
37+
* @param a First number (must be positive)
38+
* @param b Second number (must be positive)
39+
* @return true if a and b are friendly numbers, false otherwise
40+
*/
41+
public static boolean areFriendly(final int a, final int b) {
42+
if (a <= 0 || b <= 0) {
43+
return false;
44+
}
45+
final long sigmaA = sumOfDivisors(a);
46+
final long sigmaB = sumOfDivisors(b);
47+
return sigmaA * b == sigmaB * a;
48+
}
49+
}

src/main/java/com/thealgorithms/others/LowestBasePalindrome.java renamed to src/main/java/com/thealgorithms/maths/LowestBasePalindrome.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.others;
1+
package com.thealgorithms.maths;
22

33
import java.util.ArrayList;
44
import java.util.List;
@@ -23,6 +23,12 @@
2323
*
2424
* @see <a href="https://oeis.org/A016026">OEIS A016026 - Smallest base in which
2525
* n is palindromic</a>
26+
* @see com.thealgorithms.strings.Palindrome
27+
* @see com.thealgorithms.stacks.PalindromeWithStack
28+
* @see com.thealgorithms.bitmanipulation.BinaryPalindromeCheck
29+
* @see com.thealgorithms.datastructures.lists.PalindromeSinglyLinkedList
30+
* @see com.thealgorithms.maths.PalindromePrime
31+
* @see com.thealgorithms.maths.PalindromeNumber
2632
* @author TheAlgorithms Contributors
2733
*/
2834
public final class LowestBasePalindrome {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.thealgorithms.maths;
2+
3+
/**
4+
* The Padovan Sequence is a sequence of integers defined by the recurrence relation:
5+
* P(n) = P(n-2) + P(n-3) with initial values P(0) = P(1) = P(2) = 1.
6+
* Example: 1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12, 16, 21, 28, 37...
7+
*
8+
* @see <a href="https://en.wikipedia.org/wiki/Padovan_sequence">
9+
* Wikipedia: Padovan Sequence</a>
10+
* @author Vraj Prajapati (@Rosander0)
11+
*/
12+
public final class PadovanSequence {
13+
14+
private PadovanSequence() {
15+
// Utility class
16+
}
17+
18+
/**
19+
* Calculates the nth term of the Padovan Sequence.
20+
*
21+
* @param n the index of the sequence (must be non-negative)
22+
* @return the nth term of the Padovan Sequence
23+
*/
24+
public static long padovan(final int n) {
25+
if (n < 0) {
26+
throw new IllegalArgumentException("Input must be non-negative. Received: " + n);
27+
}
28+
if (n <= 2) {
29+
return 1;
30+
}
31+
long a = 1;
32+
long b = 1;
33+
long c = 1;
34+
long result = 0;
35+
for (int i = 3; i <= n; i++) {
36+
result = a + b;
37+
a = b;
38+
b = c;
39+
c = result;
40+
}
41+
return result;
42+
}
43+
}

src/main/java/com/thealgorithms/maths/PalindromeNumber.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
package com.thealgorithms.maths;
22

3+
/**
4+
* A class to check if a given number is a palindrome.
5+
* A palindromic number is a number that remains the same when its digits are reversed.
6+
*
7+
* @see com.thealgorithms.strings.Palindrome
8+
* @see com.thealgorithms.stacks.PalindromeWithStack
9+
* @see com.thealgorithms.bitmanipulation.BinaryPalindromeCheck
10+
* @see com.thealgorithms.maths.LowestBasePalindrome
11+
* @see com.thealgorithms.datastructures.lists.PalindromeSinglyLinkedList
12+
* @see com.thealgorithms.maths.PalindromePrime
13+
*/
314
public final class PalindromeNumber {
415
private PalindromeNumber() {
516
}

0 commit comments

Comments
 (0)