3

Given an array of integers, find the number of subarrays whose average is greater than or equal to K.

Constraints:

1 <= N <= 10^5
-10^9 <= A[i] <= 10^9

My solution:

If A[i] is prefix sum upto ith index in the array then

(A[j] - A[i]) / (j - i) >= K
(A[j] - A[i]) >= K * (j - i)
(A[j] - K * j) >= (A[i] - K * i) <- Let's call this expression e

So expression e tells me If I hash the running sum till the current index along with -K * current index, then all I need to search is the number of elements less expression e.

What I was mapping in hash table after processing ith index A[i] - K * i, where A[i] is running sum of the array

But I was struggling to find a data structure which can give me number of elements less than a given element in Constant time or may be O(logN) time.

Data structures that I explored to solve this problem -

  1. Segment trees but the constraints were too high for segment tree since we need to allocate the max size.
  2. Multi-sets (C++) and do the upper_bound (binary search) which would give me iterator, and to get the elements less than X I can find the difference between upper_bound and begin() iterator but runtime of this could go upto O(N) and then my overall runtime goes to O(N^2).

Note: Whatever I've mentioned in the question, considering C++ as primary language since I was solving problem in C++.

Would love to hear your thoughts/suggestions.

Ajay Kumar
  • 586
  • 5
  • 21

1 Answers1

3

You're already setting A[i] = A[i] - (K * i), so you need to find all i,j such that

A[j] - A[i] >= 0, or
A[j] >= A[i]

Assuming j>i, the number of valid pairs should just be the total number pairs minus the inversion count. You won't require any special data structure that way (an array would suffice), and inversion count calculation can be done in O(NlogN).

Abhinav Mathur
  • 7,791
  • 3
  • 10
  • 24
  • 1
    Great insight Abhinav. I missed looking at the expression this way. If we can build an array using the expression, and while traversing we are making sure that the relative ordering is preserved. So the problem is reduced to finding the number of inversion counts in the built array. Accepting the answer. Thanks Abhinav. – Ajay Kumar Nov 22 '22 at 09:24
  • new insight unlocked. ! – Ajay Kumar Nov 22 '22 at 09:27