Back to Month
MEDIUM 25 Jun 2026

3737. Count Subarrays With Majority Element I

</> Solution

class Solution {
    public int countMajoritySubarrays(int[] nums, int target) {
        int n = nums.length;
        int ans = 0;

        for (int i = 0; i < n; i++) {
            int cnt = 0;

            for (int j = i; j < n; j++) {
                if (nums[j] == target) {
                    cnt++;
                }

                int len = j - i + 1;

                if (cnt * 2 > len) {
                    ans++;
                }
            }
        }

        return ans;
    }
}

TIME COMPLEXITY

O(n²)

SPACE COMPLEXITY

O(1)