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 = 3Output:1Explanation:

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 = 3Output:2Explanation:

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:

- Iterate over the given array from
**K + 1**to the size of the array and for each element, add the previous**K**elements from the array. - Then, find the median and check if the current element is equal to or exceeds twice the value of the median. If found to be true, increase
**count**. - Finally. print the
**count**.

**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:

- Store the frequencies of the elements present in the first
**K**indices. - Iterate over the array from
**(k + 1)**index to the^{th}**N**index and for each iteration, decrease the frequency of the^{th}**i – k**where^{th}element**i**is the current index of the previous**K**trailing elements and increase the frequency count of the current element. - For each iteration obtain the value of the low median and high median which will be different if the
**K**is even. Otherwise it will be the same. - Initialize a
**count**variable that will count the frequency. Whenever**floor((k+1)/2)**is reached, the count gives a**low median**and similarly when the count reaches to**ceil((k+1)/2)**then it gives**high median**. - Then
**add**both the low and high median value and check if the current value is greater than or equal to it or and accordingly update the answer.

Below is the implementation of the above approach:

## C++

`// 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;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java Program to implement` `// the above approach` `class` `GFG{` `static` `int` `N = (` `int` `) 2e5;` `static` `int` `V = ` `500` `;` `// Function to find the count of array` `// elements >= twice the median of K` `// trailing array elements` `static` `void` `solve(` `int` `n, ` `int` `d, ` `int` `input[])` `{` ` ` `int` `[]a = ` `new` `int` `[N];` ` ` `// Stores frequencies` ` ` `int` `[]cnt = ` `new` `int` `[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` `)(Math.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` `)(Math.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` ` ` `System.out.print(answer +` `"\n"` `);` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `input[] = { ` `1` `, ` `2` `, ` `2` `, ` `4` `, ` `5` `};` ` ` `int` `n = input.length;` ` ` `int` `k = ` `3` `;` ` ` `solve(n, k, input);` `}` `}` `// This code is contributed by sapnasingh4991` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement` `# the above approach` `import` `math` `N ` `=` `200000` `V ` `=` `500` `# Function to find the count of array` `# elements >= twice the median of K` `# trailing array elements` `def` `solve(n, d, input1):` ` ` `a ` `=` `[` `0` `] ` `*` `N` ` ` `# Stores frequencies` ` ` `cnt ` `=` `[` `0` `] ` `*` `(V ` `+` `1` `)` ` ` `# Stores the array elements` ` ` `for` `i ` `in` `range` `(n):` ` ` `a[i] ` `=` `input1[i]` ` ` `answer ` `=` `0` ` ` `# Count the frequencies of the` ` ` `# array elements` ` ` `for` `i ` `in` `range` `(d):` ` ` `cnt[a[i]] ` `+` `=` `1` ` ` `# Iterating from d to n-1 index` ` ` `# means (d+1)th element to nth element` ` ` `for` `i ` `in` `range` `(d, n):` ` ` `# To check the median` ` ` `acc ` `=` `0` ` ` `low_median ` `=` `-` `1` ` ` `high_median ` `=` `-` `1` ` ` `# Iterate over the frequencies` ` ` `# of the elements` ` ` `for` `v ` `in` `range` `(V ` `+` `1` `):` ` ` `# 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` `and` ` ` `acc >` `=` `int` `(math.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` `and` ` ` `acc >` `=` `int` `(math.ceil((d ` `+` `1` `) ` `/` `2.0` `))):` ` ` `high_median ` `=` `v` ` ` `# Store 2 * median of K trailing elements` ` ` `double_median ` `=` `low_median ` `+` `high_median` ` ` `# If the current >= 2 * median` ` ` `if` `(a[i] >` `=` `double_median):` ` ` `answer ` `+` `=` `1` ` ` `# Decrease the frequency for (k-1)-th element` ` ` `cnt[a[i ` `-` `d]] ` `-` `=` `1` ` ` `# Increase the frequency of the` ` ` `# current element` ` ` `cnt[a[i]] ` `+` `=` `1` ` ` `# Print the count` ` ` `print` `(answer)` `# Driver Code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` ` ` `input1 ` `=` `[ ` `1` `, ` `2` `, ` `2` `, ` `4` `, ` `5` `]` ` ` `n ` `=` `len` `(input1)` ` ` `k ` `=` `3` ` ` `solve(n, k, input1)` `# This code is contributed by chitranayal` |

*chevron_right*

*filter_none*

## C#

`// C# Program to implement` `// the above approach` `using` `System;` `class` `GFG{` `static` `int` `N = (` `int` `) 2e5;` `static` `int` `V = 500;` `// Function to find the count of array` `// elements >= twice the median of K` `// trailing array elements` `static` `void` `solve(` `int` `n, ` `int` `d, ` `int` `[]input)` `{` ` ` `int` `[]a = ` `new` `int` `[N];` ` ` `// Stores frequencies` ` ` `int` `[]cnt = ` `new` `int` `[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` `)(Math.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` `)(Math.Ceiling((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` ` ` `Console.Write(answer + ` `"\n"` `);` `}` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` ` ` `int` `[]input = { 1, 2, 2, 4, 5 };` ` ` `int` `n = input.Length;` ` ` `int` `k = 3;` ` ` `solve(n, k, input);` `}` `}` `// This code is contributed by sapnasingh4991` |

*chevron_right*

*filter_none*

**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.

## Recommended Posts:

- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Smallest subarray of size greater than K with sum greater than a given value
- Count elements such that there are exactly X elements with values greater than or equal to X
- Count of pairs from Array with sum equal to twice their bitwise AND
- Size of smallest subarray to be removed to make count of array elements greater and smaller than K equal
- Queries to count array elements greater than or equal to a given number with updates
- Highest and Smallest power of K less than and greater than equal to N respectively
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
- Count of elements to be inserted to make Array sum twice the XOR of Array
- Sum of all array elements less than X and greater than Y for Q queries
- Count of array elements which are greater than all elements on its left
- Adding elements of an array until every element becomes greater than or equal to k
- Sum of elements in an array with frequencies greater than or equal to that element
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Count of substrings whose Decimal equivalent is greater than or equal to K
- Minimise N such that sum of count of all factors upto N is greater than or equal to X
- Check if a given number is one less than twice its reverse
- Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.