Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on Heap
  • Practice Heap
  • MCQs on Heap
  • Heap Tutorial
  • Binary Heap
  • Building Heap
  • Binomial Heap
  • Fibonacci Heap
  • Heap Sort
  • Heap vs Tree
  • Leftist Heap
  • K-ary Heap
  • Advantages & Disadvantages
Open In App
Next Article:
Count triplets (i, j, k) in an array of distinct elements such that a[i] a[k] and i < j < k
Next article icon

Count of Array elements greater than or equal to twice the Median of K trailing Array elements

Last Updated : 11 Oct, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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:  

  • 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 the count.
  • Finally. print the count. 

Below is the implementation of the above approach:  

C++14




// C++ Program to implement the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the count of
// array elements >= twice the median
// of K trailing array elements
void solve(int n, int k, int input[])
{
    // count to store ans
    int count = 0;
 
    // Iterating from k to n-1 index means
    // (k+1)th element to nth element
    for (int i = k; i < n; i++) {
 
        // temp to store the trailing k elements
        int temp[k];
        for (int j = 0; j < k; j++) {
            temp[j] = input[i - k + j];
        }
 
        // sort the array temp and
        // to find its median
        sort(temp, temp + k);
 
        int median;
 
        // find the median
        if (k % 2 == 0) {
            median = (temp[k / 2 - 1] + temp[k / 2]) / 2;
        }
        else {
            median = temp[k / 2];
        }
 
        // If the current >= 2 * median
        if (input[i] >= 2 * median) {
            count++;
        }
    }
 
    // Print the count
    cout << count << 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;
}
 
 

Java




import java.util.Arrays;
 
public class MedianComparison {
    // Function to find the count of
    // array elements >= twice the median
    // of K trailing array elements
    public static void solve(int n, int k, int[] input) {
        // Count to store the answer
        int count = 0;
 
        // Iterating from k to n-1 index means
        // (k+1)th element to nth element
        for (int i = k; i < n; i++) {
            // Temp array to store the trailing k elements
            int[] temp = new int[k];
            for (int j = 0; j < k; j++) {
                temp[j] = input[i - k + j];
            }
 
            // Sort the temp array to find its median
            Arrays.sort(temp);
 
            int median;
 
            // Find the median
            if (k % 2 == 0) {
                median = (temp[k / 2 - 1] + temp[k / 2]) / 2;
            } else {
                median = temp[k / 2];
            }
 
            // If the current element is >= 2 * median
            if (input[i] >= 2 * median) {
                count++;
            }
        }
 
        // Print the count
        System.out.println(count);
    }
 
    // 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 akshitaguprzj3
 
 

Python




# Python Program to implement the above approach
 
def solve(n, k, input_array):
    # count to store ans
    count = 0
 
    # Iterating from k to n-1 index means
    # (k+1)th element to nth element
    for i in range(k, n):
 
        # temp to store the trailing k elements
        temp = input_array[i - k:i]
 
        # sort the array temp and
        # to find its median
        temp.sort()
 
        median = 0
 
        # find the median
        if k % 2 == 0:
            median = (temp[k // 2 - 1] + temp[k // 2]) / 2
        else:
            median = temp[k // 2]
 
        # If the current >= 2 * median
        if input_array[i] >= 2 * median:
            count += 1
 
    # Print the count
    print(count)
 
# Driver Code
if __name__ == "__main__":
    input_array = [1, 2, 2, 4, 5]
    n = len(input_array)
    k = 3
 
    solve(n, k, input_array)
 
 

C#




using System;
 
class Program
{
    // Function to find the count of
    // array elements >= twice the median
    // of K trailing array elements
    static void Solve(int n, int k, int[] input)
    {
        // Count to store ans
        int count = 0;
 
        // Iterating from k to n-1 index means
        // (k+1)th element to nth element
        for (int i = k; i < n; i++)
        {
            // Temp array to store the trailing k elements
            int[] temp = new int[k];
            for (int j = 0; j < k; j++)
            {
                temp[j] = input[i - k + j];
            }
 
            // Sort the array temp and
            // to find its median
            Array.Sort(temp);
 
            int median;
 
            // Find the median
            if (k % 2 == 0)
            {
                median = (temp[k / 2 - 1] + temp[k / 2]) / 2;
            }
            else
            {
                median = temp[k / 2];
            }
 
            // If the current >= 2 * median
            if (input[i] >= 2 * median)
            {
                count++;
            }
        }
 
        // Print the count
        Console.WriteLine(count);
    }
 
    // Driver Code
    static void Main()
    {
        int[] input = { 1, 2, 2, 4, 5 };
        int n = input.Length;
        int k = 3;
 
        Solve(n, k, input);
    }
}
// This code is contributed by akshitaguprzj3
 
 

Javascript




// Function to find the count of
// array elements >= twice the median
// of K trailing array elements
function solve(n, k, input) {
    // count to store ans
    let count = 0;
 
    // Iterating from k to n-1 index means
    // (k+1)th element to nth element
    for (let i = k; i < n; i++) {
        // temp to store the trailing k elements
        let temp = [];
        for (let j = 0; j < k; j++) {
            temp.push(input[i - k + j]);
        }
 
        // sort the array temp and
        // to find its median
        temp.sort((a, b) => a - b);
 
        let median;
 
        // find the median
        if (k % 2 === 0) {
            median = (temp[k / 2 - 1] + temp[k / 2]) / 2;
        } else {
            median = temp[Math.floor(k / 2)];
        }
 
        // If the current >= 2 * median
        if (input[i] >= 2 * median) {
            count++;
        }
    }
 
    // Print the count
    console.log(count);
}
 
// Driver Code
let input = [1, 2, 2, 4, 5];
let n = input.length;
let k = 3;
 
solve(n, k, input);
 
// This code is contributed by akshitaguprzj3
 
 
Output
2         

Time Complexity: O(N * K * log K), Where N is the size of the input array and sorting requires (K * log K).
Auxiliary Space: O(K), Where K is the size of the temp array.

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)th index to the Nth index and for each iteration, decrease the frequency of the i – kth element where 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;
}
 
// This code is contributed by Sania Kumari Gupta
 
 

C




// C Program to implement the above approach
#include <stdio.h>
#include <math.h>
 
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 >= 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 >= 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
    printf("%d",answer);
}
 
// 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;
}
 
// This code is contributed by Sania Kumari Gupta
 
 

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
 
 

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
 
 

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
 
 

Javascript




<script>
 
// Javascript Program to implement
// the above approach
const N = 2e5;
const V = 500;
 
// Function to find the count of array
// elements >= twice the median of K
// trailing array elements
function solve(n, d, input)
{
    let a = new Array(N);
 
    // Stores frequencies
    let cnt = new Array(V + 1);
 
    // Stores the array elements
    for(let i = 0; i < n; ++i)
        a[i] = input[i];
 
    let answer = 0;
 
    // Count the frequencies of the
    // array elements
    for(let i = 0; i < d; ++i)
        cnt[a[i]]++;
 
    // Iterating from d to n-1 index
    // means (d+1)th element to nth element
    for(let i = d; i <= n - 1; ++i)
    {
         
        // To check the median
        let acc = 0;
 
        let low_median = -1, high_median = -1;
 
        // Iterate over the frequencies
        // of the elements
        for(let 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 >= parseInt(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 >= parseInt(Math.ceil((d + 1) / 2.0)))
                high_median = v;
        }
 
        // Store 2 * median of K trailing elements
        let 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
    document.write(answer);
}
 
// Driver Code
let input = [ 1, 2, 2, 4, 5 ];
let n = input.length;
let k = 3;
 
solve(n, k, input);
 
// This code is contributed by subham348
 
</script>
 
 
Output
2        

Time Complexity: O(N) 
Auxiliary Space: O(N)
 



Next Article
Count triplets (i, j, k) in an array of distinct elements such that a[i] a[k] and i < j < k

H

het30401
Improve
Article Tags :
  • Advanced Data Structure
  • Arrays
  • C++ Programs
  • DSA
  • Heap
  • Mathematical
  • Searching
  • Sorting
  • frequency-counting
  • median-finding
  • sliding-window
Practice Tags :
  • Advanced Data Structure
  • Arrays
  • Heap
  • Mathematical
  • Searching
  • sliding-window
  • Sorting

Similar Reads

  • Count the number of elements which are greater than any of element on right side of an array
    Given an array Arr[]. The task is to count the number of elements Arr[i] in the given array such that one or more smaller elements are present on the right side of the element Arr[i] in array. Examples: Input: Arr[] = { 3, 9, 4, 6, 7, 5 } Output: 3Numbers that counts are: 9, 6, 7 9 - As all numbers
    4 min read
  • Count K-length subarrays whose average exceeds the median of the given array
    Given an array arr[] consisting of N integers and a positive integer K, the task is to find the number of subarrays of size K whose average is greater than its median and both the average, median must be either prime or non-prime. Examples: Input: arr[] = {2, 4, 3, 5, 6}, K = 3Output: 2Explanation:F
    9 min read
  • Number of positions such that adding K to the element is greater than sum of all other elements
    Given an array arr[] and a number K. The task is to find out the number of valid positions i such that (arr[i] + K) is greater than sum of all elements of array excluding arr[i].Examples: Input: arr[] = {2, 1, 6, 7} K = 4 Output: 1 Explanation: There is only 1 valid position i.e 4th. After adding 4
    5 min read
  • Count triplets (i, j, k) in an array of distinct elements such that a[i] <a> a[k] and i < j < k
    Given an array arr[] consisting of N distinct integers, the task is to count the number of triplets (i, j, k) possible from the array arr[] such that i < j < k and arr[i] < arr[j] > arr[k]. Examples: Input: arr[] = {2, 3, 1, -1}Output: 2Explanation: From the given array, all possible tri
    8 min read
  • Minimum replacements required to have at most K distinct elements in the array
    Given an array arr[] consisting of N positive integers and an integer K, the task is to find the minimum number of array elements required to be replaced by the other array elements such that the array contains at most K distinct elements. Input: arr[] = { 1, 1, 2, 2, 5 }, K = 2 Output: 1 Explanatio
    13 min read
  • Count of subsets having sum of min and max element less than K
    Given an integer array arr[] and an integer K, the task is to find the number of non-empty subsets S such that min(S) + max(S) < K.Examples: Input: arr[] = {2, 4, 5, 7} K = 8 Output: 4 Explanation: The possible subsets are {2}, {2, 4}, {2, 4, 5} and {2, 5}Input:: arr[] = {2, 4, 2, 5, 7} K = 10 Ou
    5 min read
  • Count of subarrays of size K which is a permutation of numbers from 1 to K
    Given an array arr of distinct integers, the task is to find the count of sub-arrays of size i having all elements from 1 to i, in other words, the sub-array is any permutation of elements from 1 to i, with 1 < = i <= N. Examples: Input: arr[] = {2, 3, 1, 5, 4} Output: 3 Explanation: we have {
    6 min read
  • Finding Median of unsorted Array in linear time using C++ STL
    Given an unsorted array arr[] having N elements, the task is to find out the median of the array in linear time complexity. Examples: Input: N = 5, arr[] = {4, 1, 2, 6, 5} Output: 4 Explanation: Since N = 5, which is odd, therefore the median is the 3rd element in the sorted array. The 3rd element i
    3 min read
  • Print all array elements appearing more than N / K times
    Given an array arr[] of size N and an integer K, the task is to find all the array elements that appear more than (N / K) times. Examples: Input: arr[] = { 1, 2, 6, 6, 6, 6, 6, 10 }, K = 4Output: 6Explanation: The frequency of 6 in the array is greater than N / K(= 2). Therefore, the required output
    15+ min read
  • Frequency of an integer in the given array using Divide and Conquer
    Given an unsorted array arr[] and an integer K, the task is to count the occurrences of K in the given array using the Divide and Conquer method. Examples: Input: arr[] = {1, 1, 2, 2, 2, 2, 3}, K = 1 Output: 2 Input: arr[] = {1, 1, 2, 2, 2, 2, 3}, K = 4 Output: 0 Approach: The idea is to divide the
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences