25. Longest Subarray with Majority Greater than K
β GFG solution to Longest Subarray with Majority Greater than K: find longest subarray where count of elements > k exceeds count of elements β€ k using prefix sum transformation. π
π§© 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,
and there is no subarray of length 4 or 5 which will hold the given condition,
so the answer is 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,
so it is the longest subarray.Example 3
π Constraints
β
My Approach
Transform and Conquer
π Time and Auxiliary Space Complexity
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
πVisitor Count
Last updated