Skip to content

Conversation

Copy link

Copilot AI commented Sep 16, 2025

This PR adds a complete solution for the "Maximum Sum of Distinct Subarrays With Length K" problem, which finds the maximum subarray sum among all subarrays of length k where all elements are distinct.

Problem Description

Given an integer array nums and an integer k, find the maximum subarray sum of all subarrays that meet these conditions:

  • The subarray has exactly k elements
  • All elements in the subarray are distinct

Return the maximum sum, or 0 if no valid subarray exists.

Examples

// Example 1
nums := []int{1, 5, 4, 2, 9, 9, 9}
k := 3
// Valid subarrays: [1,5,4] (sum=10), [5,4,2] (sum=11), [4,2,9] (sum=15)
// Output: 15

// Example 2  
nums := []int{4, 4, 4}
k := 3
// No valid subarrays since all contain duplicates
// Output: 0

Solution

The implementation uses an efficient sliding window approach with O(n) time complexity:

  1. Sliding Window: Maintains a window of exactly k elements
  2. Frequency Tracking: Uses a hash map to count element occurrences in the current window
  3. Distinct Check: A window is valid only when the frequency map size equals k (all elements distinct)
  4. Sum Optimization: Tracks the maximum sum among all valid windows

Key Features

  • Efficient: O(n) time complexity, O(k) space complexity
  • Robust: Handles all edge cases including negative numbers, k > array length, and arrays with no valid subarrays
  • Well-tested: Comprehensive test suite with 10 test cases covering various scenarios
  • Standards Compliant: Follows the repository's established patterns and Go coding conventions

Files Added

  • solutions/2461_maximum_subarray_sum_distinct_elements/maximum_subarray_sum.go - Core implementation
  • solutions/2461_maximum_subarray_sum_distinct_elements/maximum_subarray_sum_test.go - Comprehensive test suite

All existing tests continue to pass, ensuring no regressions were introduced.

This pull request was created as a result of the following prompt from Copilot chat.

Solve the following LeetCode problem:

Problem: Find the maximum subarray sum of all subarrays of length k where all elements are distinct.

Description:
You are given an integer array nums and an integer k. Find the maximum subarray sum of all the subarrays of nums that meet the following conditions:

  • The length of the subarray is k, and
  • All the elements of the subarray are distinct.

Return the maximum subarray sum of all the subarrays that meet the conditions. If no subarray meets the conditions, return 0.

Examples:

  1. Input: nums = [1,5,4,2,9,9,9], k = 3
    Output: 15
    Explanation: Valid subarrays are [1,5,4] (sum=10), [5,4,2] (sum=11), [4,2,9] (sum=15)

  2. Input: nums = [4,4,4], k = 3
    Output: 0
    Explanation: No valid subarrays since all contain duplicate elements

Algorithm Approach:
Use sliding window technique with a hash map to track element frequencies:

  1. Maintain a window of size k
  2. Use a map to count occurrences of each element in the current window
  3. Track the number of distinct elements in the window
  4. If all k elements are distinct, calculate sum and update maximum
  5. Slide the window by removing the leftmost element and adding the next element

Implementation Requirements:

  • Function signature: func maximumSubarraySum(nums []int, k int) int64
  • Handle edge cases where no valid subarray exists
  • Use efficient sliding window approach with O(n) time complexity
  • Include comprehensive test cases covering all examples

💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: SupratickDey <42794642+SupratickDey@users.noreply.github.com>
Copilot AI changed the title [WIP] Maximum Subarray Sum With Length K and Distinct Elements Implement Maximum Sum of Distinct Subarrays With Length K (LeetCode 2461) Sep 16, 2025
Copilot AI requested a review from SupratickDey September 16, 2025 10:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants