
MD ARIFUL HAQUE3737. Count Subarrays With Majority Element I Difficulty: Medium Topics: Senior, Array, Hash Table,...
3737. Count Subarrays With Majority Element I
Difficulty: Medium
Topics: Senior, Array, Hash Table, Divide and Conquer, Segment Tree, Merge Sort, Counting, 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:
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]Example 2:
Example 3:
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] <= 10⁹1 <= target <= 10⁹Hint:
2 * count(target) > length
Solution:
We have developed an efficient brute-force solution that counts all subarrays where a given target appears as the majority element. Our approach iterates through every possible subarray, maintains a running count of target occurrences, and checks the majority condition using integer arithmetic to avoid floating-point precision issues. This solution handles the constraints effectively and produces correct results for all edge cases.
i in the array, we consider all subarrays that begin at i.i, we extend the subarray to every possible ending index j (from i to n-1).targetCount: number of times target appears in the current subarraylength: total number of elements in the current subarraytargetCount * 2 > length. This condition is equivalent to targetCount > length / 2 and avoids floating-point division.Let's implement this solution in PHP: 3737. Count Subarrays With Majority Element I
<?php
/**
* @param Integer[] $nums
* @param Integer $target
* @return Integer
*/
function countMajoritySubarrays(array $nums, int $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
?>
targetCount * 2 > length is mathematically equivalent to targetCount > length / 2. We use multiplication by 2 instead of division to avoid floating-point arithmetic and maintain precision with integer operations.target doesn't appear in the array, targetCount remains 0 throughout all iterations, so no subarray satisfies the majority condition, correctly yielding 0.n is the length of nums. The outer loop runs n times, and the inner loop runs n-i times for each i, resulting in n(n+1)/2 iterations total. For n = 1000, this is approximately 500,500 operations, which is efficient.count, targetCount, length, loop indices). No additional data structures are allocated that scale with input 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. ↩