3739. Count Subarrays With Majority Element II
Difficulty: Hard
Topics: Senior Staff, Array, Hash Table, Divide and Conquer, Segment Tree, Merge Sort, Prefix Sum, Biweekly Contest 169
You are given an integer array nums and an integer target.
Return the number of subarrays1 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 = 2as 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.
- Valid subarrays with
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 = 4does not appear innumsat all. Therefore, there cannot be any subarray where 4 is the majority element. Hence the answer is 0.
Constraints:
1 <= nums.length <= 10⁵1 <= nums[i] <= 10⁹1 <= target <= 10⁹
Hint:
- Convert to +1/-1: let
arr[i] = 1ifnums[i] == targetelse-1. - Build prefix sums:
pref[0]=0,pref[k] = pref[k - 1] + arr[k - 1]fork=1..n. - Count pairs
(i < j)withpref[j] > pref[i](these correspond to subarrays wheretargetis majority). - Use coordinate compression on all
prefvalues and a Fenwick tree / ordered map: iteratekfrom0..n, query how many previousprefare < current, add toans, then update. - If
targetnever appears return0.
Solution:
We solved the problem by transforming it into a prefix-sum inversion counting task. The key insight is that for target to be a majority in a subarray, the sum of +1 (for target) and -1 (for others) must be positive, which translates to finding prefix-sum pairs where the later prefix is larger than the earlier one.
Approach
- Convert
numsinto an arrayarrwheretargetbecomes+1and all other elements become-1. - Build prefix sums
pref[0..n]wherepref[i]is the sum of the firstielements ofarr. - A subarray
(i, j)(0-based) hastargetas majority iffpref[j+1] > pref[i]. - Thus, the problem reduces to counting pairs
(i, j)withi < jsuch thatpref[j] > pref[i]. - Use coordinate compression on all prefix sums to map them to 1-based indices.
- Iterate through prefix sums in order, query a Fenwick tree for how many previous prefix sums are smaller than the current one, add that count to the answer, then insert the current prefix sum into the Fenwick tree.
Let's implement this solution in PHP: 3739. Count Subarrays With Majority Element II
<?php
/**
* @param Integer[] $nums
* @param Integer $target
* @return Integer
*/
function countMajoritySubarrays(array $nums, int $target): int
{
...
...
...
/**
* go to ./solution.php
*/
}
/**
* @param $bit
* @param $idx
* @param $delta
* @return void
*/
function bitUpdate(&$bit, $idx, $delta): void
{
...
...
...
/**
* go to ./solution.php
*/
}
/**
* @param $bit
* @param $idx
* @return int|mixed
*/
function bitQuery($bit, $idx): mixed
{
...
...
...
/**
* go to ./solution.php
*/
}
/**
* Binary search helper for 0-based index
*
* @param $arr
* @param $target
* @return int
*/
function binarySearch($arr, $target): int
{
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo countMajoritySubarrays([1,2,2,3], 2) . "\n"; // Output: 5
echo countMajoritySubarrays([1,1,1,1], 1) . "\n"; // Output: 10
echo countMajoritySubarrays([1,2,3], 4) . "\n"; // Output: 0
?>
Explanation:
-
Transformation: We map
targetto+1and everything else to-1. A subarray's sum equals(#target - #non-target). For target to be majority, this sum must be> 0. -
Prefix sums: The sum of subarray
(i, j)ispref[j+1] - pref[i]. So condition becomespref[j+1] > pref[i]. -
Counting pairs: We iterate over
jfrom0ton(wherepref[j]is the prefix sum up to indexj-1), and for eachj, we count how many previousi < jhavepref[i] < pref[j]. - Fenwick tree: To efficiently count previous prefix sums less than current, we coordinate-compress all prefix sums and use a Fenwick tree (BIT) for dynamic prefix-sum queries.
-
Early exit: If
targetis not present innums, we return0immediately because it can never be a majority.
Complexity Analysis
-
Time Complexity:
O(n log n)-
O(n)to buildarrand prefix sums. -
O(n log n)for sorting prefix sums (coordinate compression) andO(n log n)for Fenwick tree operations (each query and update isO(log n)).
-
-
Space Complexity:
O(n)- We store prefix sums, compressed indices, and the Fenwick tree, all of size
O(n).
- We store prefix sums, compressed indices, and the Fenwick tree, all of size
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:
-
Subarray: A subarray is a contiguous non-empty sequence of elements within an array. ↩
Top comments (0)