refactor: MaximumSumOfDistinctSubarraysWithLengthK
(#5433)
* refactor: MaximumSumOfDistinctSubarraysWithLengthK * checkstyle: fix formatting * checkstyle: fix formatting * checkstyle: fix formatting --------- Co-authored-by: alxkm <alx@alx.com>
This commit is contained in:
parent
cd38531b0d
commit
c5b72816f3
@ -1,55 +1,53 @@
|
|||||||
package com.thealgorithms.others;
|
package com.thealgorithms.others;
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/*
|
/**
|
||||||
References: https://en.wikipedia.org/wiki/Streaming_algorithm
|
* References: https://en.wikipedia.org/wiki/Streaming_algorithm
|
||||||
* In this model, the function of interest is computing over a fixed-size window in the stream. As the stream progresses,
|
*
|
||||||
* items from the end of the window are removed from consideration while new items from the stream take their place.
|
* This model involves computing the maximum sum of subarrays of a fixed size \( K \) from a stream of integers.
|
||||||
* @author Swarga-codes (https://github.com/Swarga-codes)
|
* As the stream progresses, elements from the end of the window are removed, and new elements from the stream are added.
|
||||||
*/
|
*
|
||||||
|
* @author Swarga-codes (https://github.com/Swarga-codes)
|
||||||
|
*/
|
||||||
public final class MaximumSumOfDistinctSubarraysWithLengthK {
|
public final class MaximumSumOfDistinctSubarraysWithLengthK {
|
||||||
private MaximumSumOfDistinctSubarraysWithLengthK() {
|
private MaximumSumOfDistinctSubarraysWithLengthK() {
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
* Returns the maximum sum of subarray of size K consisting of distinct
|
/**
|
||||||
* elements.
|
* Finds the maximum sum of a subarray of size K consisting of distinct elements.
|
||||||
*
|
*
|
||||||
* @param k size of the subarray which should be considered from the given
|
* @param k The size of the subarray.
|
||||||
* array.
|
* @param nums The array from which subarrays will be considered.
|
||||||
*
|
*
|
||||||
* @param nums is the array from which we would be finding the required
|
* @return The maximum sum of any distinct-element subarray of size K. If no such subarray exists, returns 0.
|
||||||
* subarray.
|
|
||||||
*
|
|
||||||
* @return the maximum sum of distinct subarray of size K.
|
|
||||||
*/
|
*/
|
||||||
public static long maximumSubarraySum(int k, int... nums) {
|
public static long maximumSubarraySum(int k, int... nums) {
|
||||||
if (nums.length < k) {
|
if (nums.length < k) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
long max = 0; // this will store the max sum which will be our result
|
long masSum = 0; // Variable to store the maximum sum of distinct subarrays
|
||||||
long s = 0; // this will store the sum of every k elements which can be used to compare with
|
long currentSum = 0; // Variable to store the sum of the current subarray
|
||||||
// max
|
Set<Integer> currentSet = new HashSet<>(); // Set to track distinct elements in the current subarray
|
||||||
HashSet<Integer> set = new HashSet<>(); // this can be used to store unique elements in our subarray
|
|
||||||
// Looping through k elements to get the sum of first k elements
|
// Initialize the first window
|
||||||
for (int i = 0; i < k; i++) {
|
for (int i = 0; i < k; i++) {
|
||||||
s += nums[i];
|
currentSum += nums[i];
|
||||||
set.add(nums[i]);
|
currentSet.add(nums[i]);
|
||||||
}
|
}
|
||||||
// Checking if the first kth subarray contains unique elements or not if so then
|
// If the first window contains distinct elements, update maxSum
|
||||||
// we assign that to max
|
if (currentSet.size() == k) {
|
||||||
if (set.size() == k) {
|
masSum = currentSum;
|
||||||
max = s;
|
|
||||||
}
|
}
|
||||||
// Looping through the rest of the array to find different subarrays and also
|
// Slide the window across the array
|
||||||
// utilising the sliding window algorithm to find the sum
|
|
||||||
// in O(n) time complexity
|
|
||||||
for (int i = 1; i < nums.length - k + 1; i++) {
|
for (int i = 1; i < nums.length - k + 1; i++) {
|
||||||
s = s - nums[i - 1];
|
// Update the sum by removing the element that is sliding out and adding the new element
|
||||||
s = s + nums[i + k - 1];
|
currentSum = currentSum - nums[i - 1];
|
||||||
|
currentSum = currentSum + nums[i + k - 1];
|
||||||
int j = i;
|
int j = i;
|
||||||
boolean flag = false; // flag value which says that the subarray contains distinct elements
|
boolean flag = false; // flag value which says that the subarray contains distinct elements
|
||||||
while (j < i + k && set.size() < k) {
|
while (j < i + k && currentSet.size() < k) {
|
||||||
if (nums[i - 1] == nums[j]) {
|
if (nums[i - 1] == nums[j]) {
|
||||||
flag = true;
|
flag = true;
|
||||||
break;
|
break;
|
||||||
@ -58,17 +56,14 @@ public final class MaximumSumOfDistinctSubarraysWithLengthK {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!flag) {
|
if (!flag) {
|
||||||
set.remove(nums[i - 1]);
|
currentSet.remove(nums[i - 1]);
|
||||||
}
|
}
|
||||||
set.add(nums[i + k - 1]);
|
currentSet.add(nums[i + k - 1]);
|
||||||
// if the subarray contains distinct elements then we compare and update the max
|
// If the current window has distinct elements, compare and possibly update maxSum
|
||||||
// value
|
if (currentSet.size() == k && masSum < currentSum) {
|
||||||
if (set.size() == k) {
|
masSum = currentSum;
|
||||||
if (max < s) {
|
|
||||||
max = s;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
return masSum; // the final maximum sum
|
||||||
return max; // the final maximum sum
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,31 +2,21 @@ package com.thealgorithms.others;
|
|||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import java.util.stream.Stream;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
public class MaximumSumOfDistinctSubarraysWithLengthKTest {
|
public class MaximumSumOfDistinctSubarraysWithLengthKTest {
|
||||||
@Test
|
|
||||||
public void sampleTestCase1() {
|
@ParameterizedTest
|
||||||
assertEquals(15, MaximumSumOfDistinctSubarraysWithLengthK.maximumSubarraySum(3, 1, 5, 4, 2, 9, 9, 9));
|
@MethodSource("inputStream")
|
||||||
|
void testMaximumSubarraySum(int expected, int k, int[] arr) {
|
||||||
|
assertEquals(expected, MaximumSumOfDistinctSubarraysWithLengthK.maximumSubarraySum(k, arr));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
private static Stream<Arguments> inputStream() {
|
||||||
public void sampleTestCase2() {
|
return Stream.of(Arguments.of(15, 3, new int[] {1, 5, 4, 2, 9, 9, 9}), Arguments.of(0, 3, new int[] {4, 4, 4}), Arguments.of(12, 3, new int[] {9, 9, 9, 1, 2, 3}), Arguments.of(0, 0, new int[] {9, 9, 9}), Arguments.of(0, 5, new int[] {9, 9, 9}), Arguments.of(9, 1, new int[] {9, 2, 3, 7}),
|
||||||
assertEquals(0, MaximumSumOfDistinctSubarraysWithLengthK.maximumSubarraySum(3, 4, 4, 4));
|
Arguments.of(15, 5, new int[] {1, 2, 3, 4, 5}), Arguments.of(6, 3, new int[] {-1, 2, 3, 1, -2, 4}), Arguments.of(10, 1, new int[] {10}), Arguments.of(0, 2, new int[] {7, 7, 7, 7}), Arguments.of(0, 3, new int[] {}), Arguments.of(0, 10, new int[] {1, 2, 3}));
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void sampleTestCase3() {
|
|
||||||
assertEquals(12, MaximumSumOfDistinctSubarraysWithLengthK.maximumSubarraySum(3, 9, 9, 9, 1, 2, 3));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void edgeCase1() {
|
|
||||||
assertEquals(0, MaximumSumOfDistinctSubarraysWithLengthK.maximumSubarraySum(0, 9, 9, 9));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void edgeCase2() {
|
|
||||||
assertEquals(0, MaximumSumOfDistinctSubarraysWithLengthK.maximumSubarraySum(5, 9, 9, 9));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user