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. π
π§© Problem Description
π 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
β
My Approach
Prefix Sum + Hash Map
π Time and Auxiliary Space Complexity
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
πVisitor Count
Last updated