02. Longest Subarray with Majority Greater than K
β GFG solution to the Longest Subarray with Majority Greater than K problem: find maximum length subarray where count of elements > k exceeds count of elements β€ k using prefix sum technique. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given an array arr[] and an integer k. Your task is to find the length of the longest subarray in which the count of elements greater than k is more than the count of elements less than or equal to k.
A subarray is a contiguous sequence of elements within an array. The goal is to find the maximum possible length of such a subarray where elements > k have majority over elements β€ k.
π Examples
Example 1
Input: arr[] = [1, 2, 3, 4, 1], k = 2
Output: 3
Explanation: The subarray [2, 3, 4] or [3, 4, 1] satisfy the given condition.
In [2, 3, 4]: elements > 2 are [3, 4] (count = 2), elements β€ 2 are [2] (count = 1).
Since 2 > 1, this subarray is valid with length 3.Example 2
Input: arr[] = [6, 5, 3, 4], k = 2
Output: 4
Explanation: In the subarray [6, 5, 3, 4], there are 4 elements > 2 and 0 elements β€ 2.
Since 4 > 0, the entire array satisfies the condition with length 4.π Constraints
$1 \le \text{arr.size()} \le 10^6$
$1 \le \text{arr}[i] \le 10^6$
$0 \le k \le 10^6$
β
My Approach
The optimal approach uses the Prefix Sum technique with a Hash Map to efficiently track the balance between elements greater than k and elements less than or equal to k:
Prefix Sum + Hash Map
Transform Problem:
Convert each element to +1 if
arr[i] > k, otherwise -1 ifarr[i] β€ k.This transforms the problem to finding the longest subarray with positive sum.
Running Balance:
Maintain a running sum where we add +1 for elements > k and -1 for elements β€ k.
A positive sum indicates more elements > k than elements β€ k.
Hash Map for Optimization:
Store the first occurrence of each sum value with its index.
For current sum
s, if we need sum > 0, we look for previous occurrence ofs-1.This gives us the longest subarray ending at current position with positive balance.
Key Insights:
If current sum > 0, the entire prefix from start has majority > k.
If current sum β€ 0, we look for a previous position where sum was
current_sum - 1.The subarray between that position and current position will have sum = 1 (positive).
Edge Case Handling:
Initialize map with sum 0 at index -1 to handle cases where entire prefix is valid.
Only store first occurrence of each sum to maximize subarray length.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), where n is the size of the array. We make a single pass through the array, and each hash map operation takes O(1) average time.
Expected Auxiliary Space Complexity: O(n), where n is the size of the array. In the worst case, we might store all different sum values in the hash map, which can be at most n different values.
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated