Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Segment Tree
[1,2,2,3] 2
[1,1,1,1] 1
[1,2,3] 4
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3737: Count Subarrays With Majority Element I
class Solution {
public int countMajoritySubarrays(int[] nums, int target) {
int n = nums.length;
int ans = 0;
Map<Integer, Integer> cnt = new HashMap<>(n);
for (int i = 0; i < n; ++i) {
for (int j = i; j < n; ++j) {
int k = j - i + 1;
cnt.merge(nums[j], 1, Integer::sum);
if (cnt.getOrDefault(target, 0) > k / 2) {
++ans;
}
}
cnt.clear();
}
return ans;
}
}
// Accepted solution for LeetCode #3737: Count Subarrays With Majority Element I
func countMajoritySubarrays(nums []int, target int) (ans int) {
n := len(nums)
for i := range nums {
cnt := map[int]int{}
for j := i; j < n; j++ {
k := j - i + 1
cnt[nums[j]]++
if cnt[target] > k/2 {
ans++
}
}
}
return
}
# Accepted solution for LeetCode #3737: Count Subarrays With Majority Element I
class Solution:
def countMajoritySubarrays(self, nums: List[int], target: int) -> int:
n = len(nums)
ans = 0
for i in range(n):
cnt = Counter()
for j in range(i, n):
k = j - i + 1
cnt[nums[j]] += 1
if cnt[target] > k // 2:
ans += 1
return ans
// Accepted solution for LeetCode #3737: Count Subarrays With Majority Element I
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3737: Count Subarrays With Majority Element I
// class Solution {
// public int countMajoritySubarrays(int[] nums, int target) {
// int n = nums.length;
// int ans = 0;
// Map<Integer, Integer> cnt = new HashMap<>(n);
// for (int i = 0; i < n; ++i) {
// for (int j = i; j < n; ++j) {
// int k = j - i + 1;
// cnt.merge(nums[j], 1, Integer::sum);
// if (cnt.getOrDefault(target, 0) > k / 2) {
// ++ans;
// }
// }
// cnt.clear();
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3737: Count Subarrays With Majority Element I
function countMajoritySubarrays(nums: number[], target: number): number {
const n = nums.length;
let ans = 0;
for (let i = 0; i < n; ++i) {
const cnt: Record<number, number> = {};
for (let j = i; j < n; ++j) {
const k = j - i + 1;
cnt[nums[j]] = (cnt[nums[j]] || 0) + 1;
if ((cnt[target] || 0) > k >> 1) {
++ans;
}
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
For each of q queries, scan the entire range to compute the aggregate (sum, min, max). Each range scan takes up to O(n) for a full-array query. With q queries: O(n × q) total. Point updates are O(1) but queries dominate.
Building the tree is O(n). Each query or update traverses O(log n) nodes (tree height). For q queries: O(n + q log n) total. Space is O(4n) ≈ O(n) for the tree array. Lazy propagation adds O(1) per node for deferred updates.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.