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:
Alex Klymenko
2024-08-30 10:03:43 +02:00
committed by GitHub
parent cd38531b0d
commit c5b72816f3
2 changed files with 48 additions and 63 deletions

View File

@ -1,55 +1,53 @@
package com.thealgorithms.others;
import java.util.HashSet;
import java.util.Set;
/*
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.
* @author Swarga-codes (https://github.com/Swarga-codes)
*/
/**
* References: https://en.wikipedia.org/wiki/Streaming_algorithm
*
* This model involves computing the maximum sum of subarrays of a fixed size \( K \) from a stream of integers.
* 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 {
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
* array.
* @param k The size of the subarray.
* @param nums The array from which subarrays will be considered.
*
* @param nums is the array from which we would be finding the required
* subarray.
*
* @return the maximum sum of distinct subarray of size K.
* @return The maximum sum of any distinct-element subarray of size K. If no such subarray exists, returns 0.
*/
public static long maximumSubarraySum(int k, int... nums) {
if (nums.length < k) {
return 0;
}
long max = 0; // this will store the max sum which will be our result
long s = 0; // this will store the sum of every k elements which can be used to compare with
// max
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
long masSum = 0; // Variable to store the maximum sum of distinct subarrays
long currentSum = 0; // Variable to store the sum of the current subarray
Set<Integer> currentSet = new HashSet<>(); // Set to track distinct elements in the current subarray
// Initialize the first window
for (int i = 0; i < k; i++) {
s += nums[i];
set.add(nums[i]);
currentSum += nums[i];
currentSet.add(nums[i]);
}
// Checking if the first kth subarray contains unique elements or not if so then
// we assign that to max
if (set.size() == k) {
max = s;
// If the first window contains distinct elements, update maxSum
if (currentSet.size() == k) {
masSum = currentSum;
}
// Looping through the rest of the array to find different subarrays and also
// utilising the sliding window algorithm to find the sum
// in O(n) time complexity
// Slide the window across the array
for (int i = 1; i < nums.length - k + 1; i++) {
s = s - nums[i - 1];
s = s + nums[i + k - 1];
// Update the sum by removing the element that is sliding out and adding the new element
currentSum = currentSum - nums[i - 1];
currentSum = currentSum + nums[i + k - 1];
int j = i;
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]) {
flag = true;
break;
@ -58,17 +56,14 @@ public final class MaximumSumOfDistinctSubarraysWithLengthK {
}
}
if (!flag) {
set.remove(nums[i - 1]);
currentSet.remove(nums[i - 1]);
}
set.add(nums[i + k - 1]);
// if the subarray contains distinct elements then we compare and update the max
// value
if (set.size() == k) {
if (max < s) {
max = s;
}
currentSet.add(nums[i + k - 1]);
// If the current window has distinct elements, compare and possibly update maxSum
if (currentSet.size() == k && masSum < currentSum) {
masSum = currentSum;
}
}
return max; // the final maximum sum
return masSum; // the final maximum sum
}
}