Sale
Summer Offer: $190/yr $99/yr
00 : 00 : 00 : 00
Get this deal
Backend Engineer Senior programming

Given an integer array nums, return the number of contiguous subarrays that have an average value greater than or equal to the given threshold. The average is defined by the sum of the elements divided by the number of elements in the subarray. You must solve it in O(n) time complexity.


Constraints:


  • 1 <= nums.length <= 10^5

  • 1 <= nums[i] <= 100

  • 0 <= threshold <= 100


Examples:


1. Input: nums = [1, 2, 3, 4], threshold = 2
Output: 5
Explanation: Subarrays [2], [3], [4], [1, 2], and [2, 3] have averages greater than or equal to 2.
2. Input: nums = [1, 3, 2, 5, 4], threshold = 3
Output: 8
Explanation: Subarrays with averages greater than or equal to 3 are counted.
Suggested Answer

Trusted by 100+ professionals preparing for interviews

Trusted by 100+ professionals 50+ Company Question Banks 5+ Supported Languages

Practice More Questions Like This

Generate unlimited interview questions with structured answers, code runner, and AI-powered walkthroughs.

Get Started Free
Link copied to clipboard