diff --git a/problems/3719-longest-balanced-subarray-i/analysis.md b/problems/3719-longest-balanced-subarray-i/analysis.md new file mode 100644 index 0000000..7120d50 --- /dev/null +++ b/problems/3719-longest-balanced-subarray-i/analysis.md @@ -0,0 +1,58 @@ +# 3719. Longest Balanced Subarray I + +[LeetCode Link](https://leetcode.com/problems/longest-balanced-subarray-i/) + +Difficulty: Medium +Topics: Array, Hash Table, Divide and Conquer, Segment Tree, Prefix Sum +Acceptance Rate: 56.7% + +## Hints + +### Hint 1 + +Think about how you can efficiently track the count of distinct even and odd numbers as you expand a window through the array. A brute force approach checking all possible subarrays would work, but can you optimize it? + +### Hint 2 + +For each possible subarray, you need to count distinct even and odd numbers. Hash sets are perfect for tracking distinct elements. Consider iterating through all possible starting positions, and for each start, expand to the right while maintaining two sets. + +### Hint 3 + +The key insight is that with the constraint of n ≤ 1500, an O(n²) solution is acceptable. For each starting index, expand rightward while maintaining sets of distinct even and odd numbers. Check balance at each step and update the maximum length when counts match. + +## Approach + +The solution uses a nested loop approach with hash sets to track distinct elements: + +1. **Outer loop**: Iterate through each possible starting position (i) in the array +2. **Inner loop**: For each start position, expand rightward (j) to explore all subarrays starting at i +3. **Track distinct elements**: Use two sets to maintain distinct even and odd numbers in the current subarray +4. **Check balance**: After adding each element, check if the number of distinct evens equals distinct odds +5. **Update maximum**: When balanced, update the maximum length if current subarray is longer + +For example, with nums = [2,5,4,3]: +- Start at index 0 (value 2): Add to evens set {2} +- Expand to index 1 (value 5): evens {2}, odds {5} - counts equal (1,1), length = 2 +- Expand to index 2 (value 4): evens {2,4}, odds {5} - not balanced +- Expand to index 3 (value 3): evens {2,4}, odds {5,3} - balanced (2,2), length = 4 + +The algorithm continues this process for all starting positions and returns the maximum balanced subarray length found. + +## Complexity Analysis + +Time Complexity: O(n²) +- Outer loop runs n times +- Inner loop runs up to n times for each outer iteration +- Set operations (add, size check) are O(1) on average + +Space Complexity: O(n) +- Two sets that could each contain up to n/2 distinct elements in the worst case +- The sets are reset for each new starting position + +## Edge Cases + +1. **Single element array**: Cannot be balanced (need at least one even and one odd), should return appropriate minimum +2. **All even or all odd numbers**: No balanced subarray possible if all numbers have the same parity +3. **Duplicate numbers**: The problem asks for distinct counts, so [2,2,3] has 1 distinct even and 1 distinct odd +4. **Entire array is balanced**: The maximum length could be the entire array length +5. **No balanced subarray exists**: Return 0 or handle appropriately based on problem constraints diff --git a/problems/3719-longest-balanced-subarray-i/solution.go b/problems/3719-longest-balanced-subarray-i/solution.go new file mode 100644 index 0000000..00c5ace --- /dev/null +++ b/problems/3719-longest-balanced-subarray-i/solution.go @@ -0,0 +1,36 @@ +package main + +// Approach: Brute force with two sets to track distinct even and odd numbers. +// For each starting position, expand rightward and check if the subarray is balanced. +// Time: O(n²), Space: O(n) + +func longestBalancedSubarray(nums []int) int { + n := len(nums) + maxLen := 0 + + // Try all possible starting positions + for i := 0; i < n; i++ { + evens := make(map[int]bool) + odds := make(map[int]bool) + + // Expand rightward from position i + for j := i; j < n; j++ { + // Add current number to appropriate set + if nums[j]%2 == 0 { + evens[nums[j]] = true + } else { + odds[nums[j]] = true + } + + // Check if balanced (equal count of distinct evens and odds) + if len(evens) == len(odds) { + length := j - i + 1 + if length > maxLen { + maxLen = length + } + } + } + } + + return maxLen +} diff --git a/problems/3719-longest-balanced-subarray-i/solution_test.go b/problems/3719-longest-balanced-subarray-i/solution_test.go new file mode 100644 index 0000000..6bb9913 --- /dev/null +++ b/problems/3719-longest-balanced-subarray-i/solution_test.go @@ -0,0 +1,61 @@ +package main + +import "testing" + +func TestLongestBalancedSubarray(t *testing.T) { + tests := []struct { + name string + nums []int + expected int + }{ + { + name: "example 1: array with 2 distinct evens and 2 distinct odds", + nums: []int{2, 5, 4, 3}, + expected: 4, + }, + { + name: "example 2: array with duplicates", + nums: []int{3, 2, 2, 5, 4}, + expected: 5, + }, + { + name: "example 3: smaller balanced subarray", + nums: []int{1, 2, 3, 2}, + expected: 3, + }, + { + name: "edge case: single element", + nums: []int{5}, + expected: 0, + }, + { + name: "edge case: all even numbers", + nums: []int{2, 4, 6, 8}, + expected: 0, + }, + { + name: "edge case: all odd numbers", + nums: []int{1, 3, 5, 7}, + expected: 0, + }, + { + name: "edge case: two elements one even one odd", + nums: []int{2, 3}, + expected: 2, + }, + { + name: "edge case: alternating with duplicates", + nums: []int{1, 1, 2, 2}, + expected: 4, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := longestBalancedSubarray(tt.nums) + if result != tt.expected { + t.Errorf("longestBalancedSubarray(%v) = %d, want %d", tt.nums, result, tt.expected) + } + }) + } +}