Skip to content

Commit 74ca09e

Browse files
authored
Merge branch 'master' into all_construct_new_algo
2 parents 70c6189 + 94daff0 commit 74ca09e

File tree

11 files changed

+440
-13
lines changed

11 files changed

+440
-13
lines changed

DIRECTORY.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
* [IsPowerTwo](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IsPowerTwo.java)
4242
* [LowestSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java)
4343
* [ModuloPowerOfTwo](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ModuloPowerOfTwo.java)
44+
* [NextHigherSameBitCount](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NextHigherSameBitCount.java)
4445
* [NonRepeatingNumberFinder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinder.java)
4546
* [NumberAppearingOddTimes](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimes.java)
4647
* [NumbersDifferentSigns](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumbersDifferentSigns.java)
@@ -179,6 +180,7 @@
179180
* [LeftistHeap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java)
180181
* [MaxHeap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/MaxHeap.java)
181182
* [MedianFinder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/MedianFinder.java)
183+
* [MergeKSortedArrays](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/MergeKSortedArrays.java)
182184
* [MinHeap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/MinHeap.java)
183185
* [MinPriorityQueue](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/MinPriorityQueue.java)
184186
* lists
@@ -207,6 +209,7 @@
207209
* [PriorityQueues](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/queues/PriorityQueues.java)
208210
* [Queue](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/queues/Queue.java)
209211
* [QueueByTwoStacks](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/queues/QueueByTwoStacks.java)
212+
* [SlidingWindowMaximum](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/queues/SlidingWindowMaximum.java)
210213
* [TokenBucket](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/queues/TokenBucket.java)
211214
* stacks
212215
* [NodeStack](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/stacks/NodeStack.java)
@@ -328,6 +331,7 @@
328331
* [StronglyConnectedComponentOptimized](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/graph/StronglyConnectedComponentOptimized.java)
329332
* greedyalgorithms
330333
* [ActivitySelection](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/ActivitySelection.java)
334+
* [BandwidthAllocation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/BandwidthAllocation.java)
331335
* [BinaryAddition](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/BinaryAddition.java)
332336
* [CoinChange](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/CoinChange.java)
333337
* [DigitSeparation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/DigitSeparation.java)
@@ -742,6 +746,7 @@
742746
* [IsPowerTwoTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/IsPowerTwoTest.java)
743747
* [LowestSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java)
744748
* [ModuloPowerOfTwoTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ModuloPowerOfTwoTest.java)
749+
* [NextHigherSameBitCountTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NextHigherSameBitCountTest.java)
745750
* [NonRepeatingNumberFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinderTest.java)
746751
* [NumberAppearingOddTimesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimesTest.java)
747752
* [NumbersDifferentSignsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumbersDifferentSignsTest.java)
@@ -859,6 +864,7 @@
859864
* [KthElementFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/KthElementFinderTest.java)
860865
* [LeftistHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/LeftistHeapTest.java)
861866
* [MedianFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/MedianFinderTest.java)
867+
* [MergeKSortedArraysTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/MergeKSortedArraysTest.java)
862868
* [MinPriorityQueueTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/MinPriorityQueueTest.java)
863869
* lists
864870
* [CircleLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/CircleLinkedListTest.java)
@@ -882,6 +888,7 @@
882888
* [PriorityQueuesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/queues/PriorityQueuesTest.java)
883889
* [QueueByTwoStacksTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/queues/QueueByTwoStacksTest.java)
884890
* [QueueTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/queues/QueueTest.java)
891+
* [SlidingWindowMaximumTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/queues/SlidingWindowMaximumTest.java)
885892
* [TokenBucketTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/queues/TokenBucketTest.java)
886893
* stacks
887894
* [NodeStackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/stacks/NodeStackTest.java)
@@ -978,6 +985,7 @@
978985
* [StronglyConnectedComponentOptimizedTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/graph/StronglyConnectedComponentOptimizedTest.java)
979986
* greedyalgorithms
980987
* [ActivitySelectionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/ActivitySelectionTest.java)
988+
* [BandwidthAllocationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/BandwidthAllocationTest.java)
981989
* [BinaryAdditionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/BinaryAdditionTest.java)
982990
* [CoinChangeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/CoinChangeTest.java)
983991
* [DigitSeparationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationTest.java)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* This class provides a method to find the next higher number
5+
* with the same number of set bits as the given number.
6+
*
7+
* @author Hardvan
8+
*/
9+
public final class NextHigherSameBitCount {
10+
private NextHigherSameBitCount() {
11+
}
12+
13+
/**
14+
* Finds the next higher integer with the same number of set bits.
15+
* Steps:
16+
* 1. Find {@code c}, the rightmost set bit of {@code n}.
17+
* 2. Find {@code r}, the rightmost set bit of {@code n + c}.
18+
* 3. Swap the bits of {@code r} and {@code n} to the right of {@code c}.
19+
* 4. Shift the bits of {@code r} and {@code n} to the right of {@code c} to the rightmost.
20+
* 5. Combine the results of steps 3 and 4.
21+
*
22+
* @param n the input number
23+
* @return the next higher integer with the same set bit count
24+
*/
25+
public static int nextHigherSameBitCount(int n) {
26+
int c = n & -n;
27+
int r = n + c;
28+
return (((r ^ n) >> 2) / c) | r;
29+
}
30+
}
Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,64 @@
11
package com.thealgorithms.conversions;
22

3+
/**
4+
* A utility class to perform affine transformations of the form:
5+
* y = slope * x + intercept.
6+
*
7+
* This class supports inversion and composition of affine transformations.
8+
* It is immutable, meaning each instance represents a fixed transformation.
9+
*/
310
public final class AffineConverter {
411
private final double slope;
512
private final double intercept;
13+
14+
/**
15+
* Constructs an AffineConverter with the given slope and intercept.
16+
*
17+
* @param inSlope The slope of the affine transformation.
18+
* @param inIntercept The intercept (constant term) of the affine transformation.
19+
* @throws IllegalArgumentException if either parameter is NaN.
20+
*/
621
public AffineConverter(final double inSlope, final double inIntercept) {
22+
if (Double.isNaN(inSlope) || Double.isNaN(inIntercept)) {
23+
throw new IllegalArgumentException("Slope and intercept must be valid numbers.");
24+
}
725
slope = inSlope;
826
intercept = inIntercept;
927
}
1028

29+
/**
30+
* Converts the given input value using the affine transformation:
31+
* result = slope * inValue + intercept.
32+
*
33+
* @param inValue The input value to convert.
34+
* @return The transformed value.
35+
*/
1136
public double convert(final double inValue) {
1237
return slope * inValue + intercept;
1338
}
1439

40+
/**
41+
* Returns a new AffineConverter representing the inverse of the current transformation.
42+
* The inverse of y = slope * x + intercept is x = (y - intercept) / slope.
43+
*
44+
* @return A new AffineConverter representing the inverse transformation.
45+
* @throws AssertionError if the slope is zero, as the inverse would be undefined.
46+
*/
1547
public AffineConverter invert() {
16-
assert slope != 0.0;
48+
assert slope != 0.0 : "Slope cannot be zero for inversion.";
1749
return new AffineConverter(1.0 / slope, -intercept / slope);
1850
}
1951

52+
/**
53+
* Composes this affine transformation with another, returning a new AffineConverter.
54+
* If this transformation is f(x) and the other is g(x), the result is f(g(x)).
55+
*
56+
* @param other Another AffineConverter to compose with.
57+
* @return A new AffineConverter representing the composition of the two transformations.
58+
*/
2059
public AffineConverter compose(final AffineConverter other) {
21-
return new AffineConverter(slope * other.slope, slope * other.intercept + intercept);
60+
double newSlope = slope * other.slope;
61+
double newIntercept = slope * other.intercept + intercept;
62+
return new AffineConverter(newSlope, newIntercept);
2263
}
2364
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.thealgorithms.datastructures.heaps;
2+
3+
import java.util.Comparator;
4+
import java.util.PriorityQueue;
5+
6+
/**
7+
* This class provides a method to merge multiple sorted arrays into a single sorted array.
8+
* It utilizes a min-heap to efficiently retrieve the smallest elements from each array.
9+
*
10+
* Time Complexity: O(n * log k), where n is the total number of elements across all arrays
11+
* and k is the number of arrays.
12+
*
13+
* Space Complexity: O(k) for the heap, where k is the number of arrays.
14+
*
15+
* @author Hardvan
16+
*/
17+
public final class MergeKSortedArrays {
18+
private MergeKSortedArrays() {
19+
}
20+
21+
/**
22+
* Merges k sorted arrays into one sorted array using a min-heap.
23+
* Steps:
24+
* 1. Create a min-heap to store elements in the format: {value, array index, element index}
25+
* 2. Add the first element from each array to the heap
26+
* 3. While the heap is not empty, remove the smallest element from the heap
27+
* and add it to the result array. If there are more elements in the same array,
28+
* add the next element to the heap.
29+
* Continue until all elements have been processed.
30+
* The result array will contain all elements in sorted order.
31+
* 4. Return the result array.
32+
*
33+
* @param arrays a 2D array, where each subarray is sorted in non-decreasing order
34+
* @return a single sorted array containing all elements from the input arrays
35+
*/
36+
public static int[] mergeKArrays(int[][] arrays) {
37+
PriorityQueue<int[]> minHeap = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
38+
39+
int totalLength = 0;
40+
for (int i = 0; i < arrays.length; i++) {
41+
if (arrays[i].length > 0) {
42+
minHeap.offer(new int[] {arrays[i][0], i, 0});
43+
totalLength += arrays[i].length;
44+
}
45+
}
46+
47+
int[] result = new int[totalLength];
48+
int index = 0;
49+
while (!minHeap.isEmpty()) {
50+
int[] top = minHeap.poll();
51+
result[index++] = top[0];
52+
53+
if (top[2] + 1 < arrays[top[1]].length) {
54+
minHeap.offer(new int[] {arrays[top[1]][top[2] + 1], top[1], top[2] + 1});
55+
}
56+
}
57+
58+
return result;
59+
}
60+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.thealgorithms.datastructures.queues;
2+
3+
import java.util.Deque;
4+
import java.util.LinkedList;
5+
6+
/**
7+
* The {@code SlidingWindowMaximum} class provides a method to efficiently compute
8+
* the maximum element within every sliding window of size {@code k} in a given array.
9+
*
10+
* <p>The algorithm uses a deque to maintain the indices of useful elements within
11+
* the current sliding window. The time complexity of this approach is O(n) since
12+
* each element is processed at most twice.
13+
*
14+
* @author Hardvan
15+
*/
16+
public final class SlidingWindowMaximum {
17+
private SlidingWindowMaximum() {
18+
}
19+
20+
/**
21+
* Returns an array of the maximum values for each sliding window of size {@code k}.
22+
* <p>If {@code nums} has fewer elements than {@code k}, the result will be an empty array.
23+
* <p>Example:
24+
* <pre>
25+
* Input: nums = [1, 3, -1, -3, 5, 3, 6, 7], k = 3
26+
* Output: [3, 3, 5, 5, 6, 7]
27+
* </pre>
28+
*
29+
* @param nums the input array of integers
30+
* @param k the size of the sliding window
31+
* @return an array containing the maximum element for each sliding window
32+
*/
33+
public static int[] maxSlidingWindow(int[] nums, int k) {
34+
int n = nums.length;
35+
if (n < k || k == 0) {
36+
return new int[0];
37+
}
38+
39+
int[] result = new int[n - k + 1];
40+
Deque<Integer> deque = new LinkedList<>();
41+
for (int i = 0; i < n; i++) {
42+
// Remove elements from the front of the deque if they are out of the current window
43+
if (!deque.isEmpty() && deque.peekFirst() < i - k + 1) {
44+
deque.pollFirst();
45+
}
46+
47+
// Remove elements from the back if they are smaller than the current element
48+
while (!deque.isEmpty() && nums[deque.peekLast()] < nums[i]) {
49+
deque.pollLast();
50+
}
51+
52+
// Add the current element's index to the deque
53+
deque.offerLast(i);
54+
55+
// Store the maximum element for the current window (starting from the k-1th element)
56+
if (i >= k - 1) {
57+
result[i - k + 1] = nums[deque.peekFirst()];
58+
}
59+
}
60+
61+
return result;
62+
}
63+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.thealgorithms.greedyalgorithms;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Class to solve the Bandwidth Allocation Problem.
7+
* The goal is to maximize the value gained by allocating bandwidth to users.
8+
* Example:
9+
* Bandwidth = 10
10+
* Users = [3, 5, 7]
11+
* Values = [10, 20, 30]
12+
* The maximum value achievable is 40 by allocating 3 units to user 0 and 7 units to user 2.
13+
*
14+
* @author Hardvan
15+
*/
16+
public final class BandwidthAllocation {
17+
private BandwidthAllocation() {
18+
}
19+
20+
/**
21+
* Allocates bandwidth to maximize value.
22+
* Steps:
23+
* 1. Calculate the ratio of value/demand for each user.
24+
* 2. Sort the users in descending order of the ratio.
25+
* 3. Allocate bandwidth to users in order of the sorted list.
26+
* 4. If the bandwidth is not enough to allocate the full demand of a user, allocate a fraction of the demand.
27+
* 5. Return the maximum value achievable.
28+
*
29+
* @param bandwidth total available bandwidth to allocate
30+
* @param users array of user demands
31+
* @param values array of values associated with each user's demand
32+
* @return the maximum value achievable
33+
*/
34+
public static int maxValue(int bandwidth, int[] users, int[] values) {
35+
int n = users.length;
36+
double[][] ratio = new double[n][2]; // {index, ratio}
37+
38+
for (int i = 0; i < n; i++) {
39+
ratio[i][0] = i;
40+
ratio[i][1] = (double) values[i] / users[i];
41+
}
42+
43+
Arrays.sort(ratio, (a, b) -> Double.compare(b[1], a[1]));
44+
45+
int maxValue = 0;
46+
for (int i = 0; i < n; i++) {
47+
int index = (int) ratio[i][0];
48+
if (bandwidth >= users[index]) {
49+
maxValue += values[index];
50+
bandwidth -= users[index];
51+
} else {
52+
maxValue += (int) (ratio[i][1] * bandwidth);
53+
break;
54+
}
55+
}
56+
return maxValue;
57+
}
58+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.CsvSource;
7+
8+
class NextHigherSameBitCountTest {
9+
10+
@ParameterizedTest
11+
@CsvSource({
12+
"5, 6", // 101 -> 110
13+
"7, 11", // 0111 -> 1011
14+
"3, 5", // 011 -> 101
15+
"12, 17", // 001100 -> 010001
16+
"15, 23" // 01111 -> 10111
17+
})
18+
void
19+
testNextHigherSameBitCount(int input, int expected) {
20+
assertEquals(expected, NextHigherSameBitCount.nextHigherSameBitCount(input));
21+
}
22+
}

0 commit comments

Comments
 (0)