Skip to content

Commit c5b7281

Browse files
alxkmalxkm
and
alxkm
authored
refactor: MaximumSumOfDistinctSubarraysWithLengthK (#5433)
* refactor: MaximumSumOfDistinctSubarraysWithLengthK * checkstyle: fix formatting * checkstyle: fix formatting * checkstyle: fix formatting --------- Co-authored-by: alxkm <[email protected]>
1 parent cd38531 commit c5b7281

File tree

2 files changed

+47
-62
lines changed

2 files changed

+47
-62
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,53 @@
11
package com.thealgorithms.others;
22

33
import java.util.HashSet;
4+
import java.util.Set;
45

5-
/*
6-
References: https://en.wikipedia.org/wiki/Streaming_algorithm
7-
* In this model, the function of interest is computing over a fixed-size window in the stream. As the stream progresses,
8-
* items from the end of the window are removed from consideration while new items from the stream take their place.
9-
* @author Swarga-codes (https://github.com/Swarga-codes)
10-
*/
6+
/**
7+
* References: https://en.wikipedia.org/wiki/Streaming_algorithm
8+
*
9+
* This model involves computing the maximum sum of subarrays of a fixed size \( K \) from a stream of integers.
10+
* As the stream progresses, elements from the end of the window are removed, and new elements from the stream are added.
11+
*
12+
* @author Swarga-codes (https://github.com/Swarga-codes)
13+
*/
1114
public final class MaximumSumOfDistinctSubarraysWithLengthK {
1215
private MaximumSumOfDistinctSubarraysWithLengthK() {
1316
}
14-
/*
15-
* Returns the maximum sum of subarray of size K consisting of distinct
16-
* elements.
17-
*
18-
* @param k size of the subarray which should be considered from the given
19-
* array.
17+
18+
/**
19+
* Finds the maximum sum of a subarray of size K consisting of distinct elements.
2020
*
21-
* @param nums is the array from which we would be finding the required
22-
* subarray.
21+
* @param k The size of the subarray.
22+
* @param nums The array from which subarrays will be considered.
2323
*
24-
* @return the maximum sum of distinct subarray of size K.
24+
* @return The maximum sum of any distinct-element subarray of size K. If no such subarray exists, returns 0.
2525
*/
2626
public static long maximumSubarraySum(int k, int... nums) {
2727
if (nums.length < k) {
2828
return 0;
2929
}
30-
long max = 0; // this will store the max sum which will be our result
31-
long s = 0; // this will store the sum of every k elements which can be used to compare with
32-
// max
33-
HashSet<Integer> set = new HashSet<>(); // this can be used to store unique elements in our subarray
34-
// Looping through k elements to get the sum of first k elements
30+
long masSum = 0; // Variable to store the maximum sum of distinct subarrays
31+
long currentSum = 0; // Variable to store the sum of the current subarray
32+
Set<Integer> currentSet = new HashSet<>(); // Set to track distinct elements in the current subarray
33+
34+
// Initialize the first window
3535
for (int i = 0; i < k; i++) {
36-
s += nums[i];
37-
set.add(nums[i]);
36+
currentSum += nums[i];
37+
currentSet.add(nums[i]);
3838
}
39-
// Checking if the first kth subarray contains unique elements or not if so then
40-
// we assign that to max
41-
if (set.size() == k) {
42-
max = s;
39+
// If the first window contains distinct elements, update maxSum
40+
if (currentSet.size() == k) {
41+
masSum = currentSum;
4342
}
44-
// Looping through the rest of the array to find different subarrays and also
45-
// utilising the sliding window algorithm to find the sum
46-
// in O(n) time complexity
43+
// Slide the window across the array
4744
for (int i = 1; i < nums.length - k + 1; i++) {
48-
s = s - nums[i - 1];
49-
s = s + nums[i + k - 1];
45+
// Update the sum by removing the element that is sliding out and adding the new element
46+
currentSum = currentSum - nums[i - 1];
47+
currentSum = currentSum + nums[i + k - 1];
5048
int j = i;
5149
boolean flag = false; // flag value which says that the subarray contains distinct elements
52-
while (j < i + k && set.size() < k) {
50+
while (j < i + k && currentSet.size() < k) {
5351
if (nums[i - 1] == nums[j]) {
5452
flag = true;
5553
break;
@@ -58,17 +56,14 @@ public static long maximumSubarraySum(int k, int... nums) {
5856
}
5957
}
6058
if (!flag) {
61-
set.remove(nums[i - 1]);
59+
currentSet.remove(nums[i - 1]);
6260
}
63-
set.add(nums[i + k - 1]);
64-
// if the subarray contains distinct elements then we compare and update the max
65-
// value
66-
if (set.size() == k) {
67-
if (max < s) {
68-
max = s;
69-
}
61+
currentSet.add(nums[i + k - 1]);
62+
// If the current window has distinct elements, compare and possibly update maxSum
63+
if (currentSet.size() == k && masSum < currentSum) {
64+
masSum = currentSum;
7065
}
7166
}
72-
return max; // the final maximum sum
67+
return masSum; // the final maximum sum
7368
}
7469
}

src/test/java/com/thealgorithms/others/MaximumSumOfDistinctSubarraysWithLengthKTest.java

+11-21
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,21 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5-
import org.junit.jupiter.api.Test;
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
69

710
public class MaximumSumOfDistinctSubarraysWithLengthKTest {
8-
@Test
9-
public void sampleTestCase1() {
10-
assertEquals(15, MaximumSumOfDistinctSubarraysWithLengthK.maximumSubarraySum(3, 1, 5, 4, 2, 9, 9, 9));
11-
}
12-
13-
@Test
14-
public void sampleTestCase2() {
15-
assertEquals(0, MaximumSumOfDistinctSubarraysWithLengthK.maximumSubarraySum(3, 4, 4, 4));
16-
}
17-
18-
@Test
19-
public void sampleTestCase3() {
20-
assertEquals(12, MaximumSumOfDistinctSubarraysWithLengthK.maximumSubarraySum(3, 9, 9, 9, 1, 2, 3));
21-
}
2211

23-
@Test
24-
public void edgeCase1() {
25-
assertEquals(0, MaximumSumOfDistinctSubarraysWithLengthK.maximumSubarraySum(0, 9, 9, 9));
12+
@ParameterizedTest
13+
@MethodSource("inputStream")
14+
void testMaximumSubarraySum(int expected, int k, int[] arr) {
15+
assertEquals(expected, MaximumSumOfDistinctSubarraysWithLengthK.maximumSubarraySum(k, arr));
2616
}
2717

28-
@Test
29-
public void edgeCase2() {
30-
assertEquals(0, MaximumSumOfDistinctSubarraysWithLengthK.maximumSubarraySum(5, 9, 9, 9));
18+
private static Stream<Arguments> inputStream() {
19+
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}),
20+
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}));
3121
}
3222
}

0 commit comments

Comments
 (0)