Given an array A[] of size greater than integer K, the task is to find the total number of elements from the array which are greater than or equal to twice the median of K trailing elements in the given array.
Examples:
_Input: _A[] = {10, 20, 30, 40, 50}, K = 3
_Output: _1
Explanation:
Since K = 3, the only two elements to be checked are {40, 50}.
For 40, the median of 3 trailing numbers {10, 20, 30} is 20.
Since 40 = 2 * 20, 40 is counted.
For element 50 the median of 3 trailing numbers {20, 30, 40} is 30.
Since 50 < 2 * 30, so 50 is not counted.
Therefore, the answer is 1.
_Input: _A[] = {1, 2, 2, 4, 5}, K = 3
_Output: _2
Explanation:
Since K = 3, the only two elements considered are {4, 5}.
For 4, the median of 3 trailing numbers {1, 2, 2} is 2.
Since 4 = 2 * 2, therefore, 4 is counted.
For 5 the median of 3 trailing numbers {2, 2, 4} is 2.
5 > 2 * 2, so 5 is counted.
Therefore, the answer is 2.
Naive Approach:
Follow the steps below to solve the problem:
Time Complexity:_ O(N * K * log K)_
Auxiliary Space:_ O(1)_
Efficient Approach:
To optimize the above approach, the idea is to use frequency-counting and sliding window technique. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
// C++ Program to implement
// the above approach
#include <bits/stdc++.h>
**using**
**namespace**
std;
**const**
**int**
N = 2e5;
**const**
**int**
V = 500;
// Function to find the count of array
// elements >= twice the median of K
// trailing array elements
**void**
solve(``**int**
n,
**int**
d,
**int**
input[])
{
**int**
a[N];
// Stores frequencies
**int**
cnt[V + 1];
// Stores the array elements
**for**
(``**int**
i = 0; i < n; ++i)
a[i] = input[i];
**int**
answer = 0;
// Count the frequencies of the
// array elements
**for**
(``**int**
i = 0; i < d; ++i)
cnt[a[i]]++;
// Iterating from d to n-1 index
// means (d+1)th element to nth element
**for**
(``**int**
i = d; i <= n - 1; ++i) {
// To check the median
**int**
acc = 0;
**int**
low_median = -1, high_median = -1;
// Iterate over the frequencies
// of the elements
**for**
(``**int**
v = 0; v <= V; ++v) {
// Add the frequencies
acc += cnt[v];
// Check if the low_median value is
// obtained or not, if yes then do
// not change as it will be minimum
**if**
(low_median == -1
&& acc >=
**int**``(``**floor**``((d + 1) / 2.0)))
low_median = v;
// Check if the high_median value is
// obtained or not, if yes then do not
// change it as it will be maximum
**if**
(high_median == -1
&& acc >=
**int**``(``**ceil**``((d + 1) / 2.0)))
high_median = v;
}
// Store 2 * median of K trailing elements
**int**
double_median = low_median + high_median;
// If the current >= 2 * median
**if**
(a[i] >= double_median)
answer++;
// Decrease the frequency for (k-1)-th element
cnt[a[i - d]]--;
// Increase the frequency of the
// current element
cnt[a[i]]++;
}
// Print the count
cout << answer << endl;
}
// Driver Code
**int**
main()
{
**int**
input[] = { 1, 2, 2, 4, 5 };
**int**
n =
**sizeof**
input /
**sizeof**
input[0];
**int**
k = 3;
solve(n, k, input);
**return**
0;
}
Output:
2
Time Complexity:_ O(N)_
Auxiliary Space:_ O(N)_
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
#arrays #mathematical #searching #frequency-counting #median-finding #sliding-window