Watch 6 video solutions for Count Subarrays With Majority Element I, a medium level problem involving Array, Hash Table, Divide and Conquer. This walkthrough by Programming Live with Larry has 371 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given an integer array nums and an integer target.
Return the number of subarrays of nums in which target is the majority element.
The majority element of a subarray is the element that appears strictly more than half of the times in that subarray.
Example 1:
Input: nums = [1,2,2,3], target = 2
Output: 5
Explanation:
Valid subarrays with target = 2 as the majority element:
nums[1..1] = [2]nums[2..2] = [2]nums[1..2] = [2,2]nums[0..2] = [1,2,2]nums[1..3] = [2,2,3]So there are 5 such subarrays.
Example 2:
Input: nums = [1,1,1,1], target = 1
Output: 10
Explanation:
All 10 subarrays have 1 as the majority element.
Example 3:
Input: nums = [1,2,3], target = 4
Output: 0
Explanation:
target = 4 does not appear in nums at all. Therefore, there cannot be any subarray where 4 is the majority element. Hence the answer is 0.
Constraints:
1 <= nums.length <= 10001 <= nums[i] <= 1091 <= target <= 109Problem Overview: You are given an array and must count how many subarrays contain a majority element, meaning one value appears strictly more than half of the subarray length. The challenge is efficiently checking the majority condition across all possible subarrays.
Approach 1: Brute Force Enumeration (O(n^3) time, O(1) space)
The most direct strategy enumerates every subarray using two nested loops and then checks whether a majority element exists inside it. For each subarray [i, j], count frequencies of elements and verify whether any value occurs more than (j - i + 1) / 2 times. This requires scanning the subarray again to compute counts, resulting in O(n^3) time. It is straightforward and useful for understanding the majority definition but becomes impractical for large arrays.
Approach 2: Optimized Enumeration with Frequency Map (O(n^2) time, O(n) space)
A better solution still enumerates all subarrays but avoids recomputing frequencies from scratch. Fix the starting index i, then extend the right boundary j one step at a time. Maintain a running frequency map using a hash table and track the current maximum frequency. For each extension, update the count of nums[j], update the max frequency, and check whether maxFreq * 2 > (j - i + 1). This converts the inner work from scanning the entire subarray to constant-time updates, reducing total complexity to O(n^2). This approach is practical for medium input sizes and commonly appears in interview-style enumeration problems.
Approach 3: Prefix Balance Transformation (Conceptual Optimization)
For deeper optimization, the majority condition can be transformed using prefix balances. For a chosen candidate value, convert the array into +1 (candidate) and -1 (others). A subarray has that candidate as majority if its transformed sum is positive. Using techniques from prefix sum and counting valid prefix differences (sometimes with divide and conquer or merge-based counting), you can count qualifying segments more efficiently for each candidate. While more complex, this idea connects majority detection with classic prefix comparison problems.
Recommended for interviews: The optimized enumeration approach is the most practical answer. It demonstrates understanding of subarray enumeration, incremental frequency updates, and hash-based counting. Mentioning the brute force approach shows baseline reasoning, while the optimized O(n^2) solution proves you can reduce redundant work.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Enumeration | O(n^3) | O(1) | Good for understanding the majority condition or very small arrays |
| Enumeration with Frequency Map | O(n^2) | O(n) | General solution for medium constraints and typical interview answers |
| Prefix Balance / Counting Technique | O(n log n) or better depending on implementation | O(n) | Advanced optimization using prefix sums and divide-and-conquer counting |