Skip to content

Commit 04e9eb5

Browse files
committed
2 parents 1d3f798 + f325279 commit 04e9eb5

26 files changed

Lines changed: 2414 additions & 1773 deletions

.github/CODEOWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
* @DenizAltunkapan @yanglbme @vil02 @BamaCharanChhandogi @alxkm @siriak
1+
* @DenizAltunkapan @yanglbme @vil02 @alxkm

.github/workflows/update_directory.yml

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

DIRECTORY.md

Lines changed: 1418 additions & 1402 deletions
Large diffs are not rendered by default.

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<dependency>
2121
<groupId>org.junit</groupId>
2222
<artifactId>junit-bom</artifactId>
23-
<version>5.13.2</version>
23+
<version>5.13.3</version>
2424
<type>pom</type>
2525
<scope>import</scope>
2626
</dependency>
@@ -48,7 +48,7 @@
4848
<dependency>
4949
<groupId>org.apache.commons</groupId>
5050
<artifactId>commons-lang3</artifactId>
51-
<version>3.17.0</version>
51+
<version>3.18.0</version>
5252
</dependency>
5353
<dependency>
5454
<groupId>org.apache.commons</groupId>
@@ -113,7 +113,7 @@
113113
<dependency>
114114
<groupId>com.puppycrawl.tools</groupId>
115115
<artifactId>checkstyle</artifactId>
116-
<version>10.26.0</version>
116+
<version>10.26.1</version>
117117
</dependency>
118118
</dependencies>
119119
</plugin>

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
package com.thealgorithms.bitmanipulation;
22

3+
/**
4+
* Utility class for performing bit-swapping operations on integers.
5+
* This class cannot be instantiated.
6+
*/
37
public final class BitSwap {
48
private BitSwap() {
59
}
6-
/*
7-
* @brief Swaps the bits at the position posA and posB from data
10+
11+
/**
12+
* Swaps two bits at specified positions in an integer.
13+
*
14+
* @param data The input integer whose bits need to be swapped
15+
* @param posA The position of the first bit (0-based, from least significant)
16+
* @param posB The position of the second bit (0-based, from least significant)
17+
* @return The modified value with swapped bits
18+
* @throws IllegalArgumentException if either position is negative or ≥ 32
819
*/
920
public static int bitSwap(int data, final int posA, final int posB) {
21+
if (posA < 0 || posA >= Integer.SIZE || posB < 0 || posB >= Integer.SIZE) {
22+
throw new IllegalArgumentException("Bit positions must be between 0 and 31");
23+
}
24+
1025
if (SingleBitOperations.getBit(data, posA) != SingleBitOperations.getBit(data, posB)) {
1126
data ^= (1 << posA) ^ (1 << posB);
1227
}
Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,43 @@
11
package com.thealgorithms.maths;
22

33
/**
4-
* https://en.wikipedia.org/wiki/Pythagorean_triple
4+
* Utility class to check if three integers form a Pythagorean triple.
5+
* A Pythagorean triple consists of three positive integers a, b, and c,
6+
* such that a² + b² = c².
7+
*
8+
* Common examples:
9+
* - (3, 4, 5)
10+
* - (5, 12, 13)
11+
*
12+
* Reference: https://en.wikipedia.org/wiki/Pythagorean_triple
513
*/
614
public final class PythagoreanTriple {
7-
private PythagoreanTriple() {
8-
}
915

10-
public static void main(String[] args) {
11-
assert isPythagTriple(3, 4, 5);
12-
assert isPythagTriple(5, 12, 13);
13-
assert isPythagTriple(6, 8, 10);
14-
assert !isPythagTriple(10, 20, 30);
15-
assert !isPythagTriple(6, 8, 100);
16-
assert !isPythagTriple(-1, -1, 1);
16+
private PythagoreanTriple() {
1717
}
1818

1919
/**
20-
* Check if a,b,c are a Pythagorean Triple
20+
* Checks whether three integers form a Pythagorean triple.
21+
* The order of parameters does not matter.
2122
*
22-
* @param a x/y component length of a right triangle
23-
* @param b y/x component length of a right triangle
24-
* @param c hypotenuse length of a right triangle
25-
* @return boolean <tt>true</tt> if a, b, c satisfy the Pythagorean theorem,
26-
* otherwise
27-
* <tt>false</tt>
23+
* @param a one side length
24+
* @param b another side length
25+
* @param c another side length
26+
* @return {@code true} if (a, b, c) can form a Pythagorean triple, otherwise {@code false}
2827
*/
2928
public static boolean isPythagTriple(int a, int b, int c) {
3029
if (a <= 0 || b <= 0 || c <= 0) {
3130
return false;
32-
} else {
33-
return (a * a) + (b * b) == (c * c);
3431
}
32+
33+
// Sort the sides so the largest is treated as hypotenuse
34+
int[] sides = {a, b, c};
35+
java.util.Arrays.sort(sides);
36+
37+
int x = sides[0];
38+
int y = sides[1];
39+
int hypotenuse = sides[2];
40+
41+
return x * x + y * y == hypotenuse * hypotenuse;
3542
}
3643
}

src/main/java/com/thealgorithms/misc/MapReduce.java

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,34 @@
77
import java.util.function.Function;
88
import java.util.stream.Collectors;
99

10-
/*
11-
* MapReduce is a programming model for processing and generating large data sets with a parallel,
12-
distributed algorithm on a cluster.
13-
* It has two main steps: the Map step, where the data is divided into smaller chunks and processed in parallel,
14-
and the Reduce step, where the results from the Map step are combined to produce the final output.
15-
* Wikipedia link : https://en.wikipedia.org/wiki/MapReduce
16-
*/
17-
10+
/**
11+
* MapReduce is a programming model for processing and generating large data sets
12+
* using a parallel, distributed algorithm on a cluster.
13+
* It consists of two main phases:
14+
* - Map: the input data is split into smaller chunks and processed in parallel.
15+
* - Reduce: the results from the Map phase are aggregated to produce the final output.
16+
*
17+
* See also: https://en.wikipedia.org/wiki/MapReduce
18+
*/
1819
public final class MapReduce {
20+
1921
private MapReduce() {
2022
}
21-
/*
22-
*Counting all the words frequency within a sentence.
23-
*/
24-
public static String mapreduce(String sentence) {
25-
List<String> wordList = Arrays.stream(sentence.split(" ")).toList();
2623

27-
// Map step
28-
Map<String, Long> wordCounts = wordList.stream().collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()));
29-
30-
// Reduce step
31-
StringBuilder result = new StringBuilder();
32-
wordCounts.forEach((word, count) -> result.append(word).append(": ").append(count).append(","));
24+
/**
25+
* Counts the frequency of each word in a given sentence using a simple MapReduce-style approach.
26+
*
27+
* @param sentence the input sentence
28+
* @return a string representing word frequencies in the format "word: count,word: count,..."
29+
*/
30+
public static String countWordFrequencies(String sentence) {
31+
// Map phase: split the sentence into words
32+
List<String> words = Arrays.asList(sentence.trim().split("\\s+"));
3333

34-
// Removing the last ',' if it exists
35-
if (!result.isEmpty()) {
36-
result.setLength(result.length() - 1);
37-
}
34+
// Group and count occurrences of each word, maintain insertion order
35+
Map<String, Long> wordCounts = words.stream().collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()));
3836

39-
return result.toString();
37+
// Reduce phase: format the result
38+
return wordCounts.entrySet().stream().map(entry -> entry.getKey() + ": " + entry.getValue()).collect(Collectors.joining(","));
4039
}
4140
}

src/main/java/com/thealgorithms/misc/MedianOfRunningArray.java

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,74 @@
44
import java.util.PriorityQueue;
55

66
/**
7-
* @author shrutisheoran
7+
* A generic abstract class to compute the median of a dynamically growing stream of numbers.
8+
*
9+
* @param <T> the number type, must extend Number and be Comparable
10+
*
11+
* Usage:
12+
* Extend this class and implement {@code calculateAverage(T a, T b)} to define how averaging is done.
813
*/
914
public abstract class MedianOfRunningArray<T extends Number & Comparable<T>> {
1015

11-
private PriorityQueue<T> maxHeap;
12-
private PriorityQueue<T> minHeap;
16+
private final PriorityQueue<T> maxHeap; // Lower half (max-heap)
17+
private final PriorityQueue<T> minHeap; // Upper half (min-heap)
1318

14-
// Constructor
1519
public MedianOfRunningArray() {
16-
this.maxHeap = new PriorityQueue<>(Collections.reverseOrder()); // Max Heap
17-
this.minHeap = new PriorityQueue<>(); // Min Heap
20+
this.maxHeap = new PriorityQueue<>(Collections.reverseOrder());
21+
this.minHeap = new PriorityQueue<>();
1822
}
1923

20-
/*
21-
Inserting lower half of array to max Heap
22-
and upper half to min heap
24+
/**
25+
* Inserts a new number into the data structure.
26+
*
27+
* @param element the number to insert
2328
*/
24-
public void insert(final T e) {
25-
if (!minHeap.isEmpty() && e.compareTo(minHeap.peek()) < 0) {
26-
maxHeap.offer(e);
27-
if (maxHeap.size() > minHeap.size() + 1) {
28-
minHeap.offer(maxHeap.poll());
29-
}
29+
public final void insert(final T element) {
30+
if (!minHeap.isEmpty() && element.compareTo(minHeap.peek()) < 0) {
31+
maxHeap.offer(element);
32+
balanceHeapsIfNeeded();
3033
} else {
31-
minHeap.offer(e);
32-
if (minHeap.size() > maxHeap.size() + 1) {
33-
maxHeap.offer(minHeap.poll());
34-
}
34+
minHeap.offer(element);
35+
balanceHeapsIfNeeded();
3536
}
3637
}
3738

38-
/*
39-
Returns median at any given point
39+
/**
40+
* Returns the median of the current elements.
41+
*
42+
* @return the median value
43+
* @throws IllegalArgumentException if no elements have been inserted
4044
*/
41-
public T median() {
45+
public final T getMedian() {
4246
if (maxHeap.isEmpty() && minHeap.isEmpty()) {
43-
throw new IllegalArgumentException("Enter at least 1 element, Median of empty list is not defined!");
44-
} else if (maxHeap.size() == minHeap.size()) {
45-
T maxHeapTop = maxHeap.peek();
46-
T minHeapTop = minHeap.peek();
47-
return calculateAverage(maxHeapTop, minHeapTop);
47+
throw new IllegalArgumentException("Median is undefined for an empty data set.");
4848
}
49-
return maxHeap.size() > minHeap.size() ? maxHeap.peek() : minHeap.peek();
49+
50+
if (maxHeap.size() == minHeap.size()) {
51+
return calculateAverage(maxHeap.peek(), minHeap.peek());
52+
}
53+
54+
return (maxHeap.size() > minHeap.size()) ? maxHeap.peek() : minHeap.peek();
5055
}
5156

52-
public abstract T calculateAverage(T a, T b);
57+
/**
58+
* Calculates the average between two values.
59+
* Concrete subclasses must define how averaging works (e.g., for Integer, Double, etc.).
60+
*
61+
* @param a first number
62+
* @param b second number
63+
* @return the average of a and b
64+
*/
65+
protected abstract T calculateAverage(T a, T b);
66+
67+
/**
68+
* Balances the two heaps so that their sizes differ by at most 1.
69+
*/
70+
private void balanceHeapsIfNeeded() {
71+
if (maxHeap.size() > minHeap.size() + 1) {
72+
minHeap.offer(maxHeap.poll());
73+
} else if (minHeap.size() > maxHeap.size() + 1) {
74+
maxHeap.offer(minHeap.poll());
75+
}
76+
}
5377
}

0 commit comments

Comments
 (0)