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:
Largest triplet product in a stream
Next article icon

Find Median from Running Data Stream

Last Updated : 17 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

Given a data stream arr[] where integers are read sequentially, the task is to determine the median of the elements encountered so far after each new integer is read.

There are two cases for median on the basis of data set size.

  • If the data set has an odd number then the middle one will be consider as median.
  • If the data set has an even number then there is no distinct middle value and the median will be the arithmetic mean of the two middle values.

Example:

Input: arr[] = [5, 15, 1, 3, 2, 8]
Output: [5.00, 10.00, 5.00, 4.00, 3.00, 4.00]
Explanation:
After reading 1st element of stream – 5 -> median = 5
After reading 2nd element of stream – 5, 15 -> median = (5+15)/2 = 10
After reading 3rd element of stream – 5, 15, 1 -> median = 5
After reading 4th element of stream – 5, 15, 1, 3 -> median = (3+5)/2 = 4
After reading 5th element of stream – 5, 15, 1, 3, 2 -> median = 3
After reading 6th element of stream – 5, 15, 1, 3, 2, 8 -> median = (3+5)/2 = 4

Input: arr[] = [2, 2, 2, 2]
Output: [2.00, 2.00, 2.00, 2.00]
Explanation:
After reading 1st element of stream – 2 -> median = 2
After reading 2nd element of stream – 2, 2 -> median = (2+2)/2 = 2
After reading 3rd element of stream – 2, 2, 2 -> median = 2
After reading 4th element of stream – 2, 2, 2, 2 -> median = (2+2)/2 = 2

Table of Content

  • [Naive Approach] Using Insertion Sort
  • [Expected Approach] Using Heaps

[Naive Approach] Using Insertion Sort

If we can sort the data as it appears, we can easily locate the median (center) element. Insertion Sort is one such online algorithm that sorts the data appeared so far. At any instance of sorting, say after sorting i-th element, the first i elements of the array are sorted. The insertion sort doesn’t depend on future data to sort data input till that point. In other words, insertion sort considers data sorted so far while inserting the next element. This is the key part of insertion sort that makes it an onlinealgorithm.

C++
// C++ program to find Median from Running Data Stream // Using Insertion Sort #include <iostream> #include <vector> #include <iomanip> using namespace std;  // Function to print median of stream of integers vector<double> getMedian(vector<int> &arr) {     vector<double> res;      res.push_back(arr[0]);      for (int i = 1; i < arr.size(); i++) {         int j = i - 1;         int num = arr[i];          // shift elements to right to create space to insert         // the current element at its correct position         while (j >= 0 && arr[j] > num) {             arr[j + 1] = arr[j];             j--;         }         arr[j + 1] = num;                  int len = i + 1;         double median;          // If odd number of integers are read from stream         // then middle element in sorted order is median         // else average of middle elements is median         if (len % 2 != 0) {             median = arr[len / 2];         }         else {             median = (double)(arr[(len / 2) - 1] + arr[len / 2]) / 2;         }          res.push_back(median);     }          return res; }  int main() {     vector<int> arr = {5, 15, 1, 3, 2, 8};     vector<double> res = getMedian(arr);     cout << fixed << setprecision(2);          for (double median: res)          cout << median << " ";     return 0; } 
Java
// Java program to find Median from Running Data Stream // Using Insertion Sort import java.util.ArrayList; import java.util.Arrays;  class GfG {          // Function to print median of stream of integers     static ArrayList<Double> getMedian(int[] arr) {         ArrayList<Double> res = new ArrayList<>();         res.add((double) arr[0]);          for (int i = 1; i < arr.length; i++) {             int j = i - 1;             int num = arr[i];              // shift elements to right to create space to insert             // the current element at its correct position             while (j >= 0 && arr[j] > num) {                 arr[j + 1] = arr[j];                 j--;             }             arr[j + 1] = num;              int len = i + 1;             double median;              // If odd number of integers are read from stream             // then middle element in sorted order is median             // else average of middle elements is median             if (len % 2 != 0) {                 median = arr[len / 2];             } else {                 median = (arr[(len / 2) - 1] + arr[len / 2]) / 2.0;             }              res.add(median);         }          return res;     }      public static void main(String[] args) {         int[] arr = {5, 15, 1, 3, 2, 8};         ArrayList<Double> res = getMedian(arr);                  for (int i = 0; i < res.size(); i++) {             System.out.printf("%.2f ", res.get(i));         }     } } 
Python
# Python program to find Median from Running Data Stream # Using Insertion Sort  def getMedian(arr):     res = []          res.append(float(arr[0]))      for i in range(1, len(arr)):         j = i - 1         num = arr[i]          # shift elements to right to create space to insert         # the current element at its correct position         while j >= 0 and arr[j] > num:             arr[j + 1] = arr[j]             j -= 1         arr[j + 1] = num          length = i + 1          # If odd number of integers are read from stream         # then middle element in sorted order is median         # else average of middle elements is median         if length % 2 != 0:             median = arr[length // 2]         else:             median = (arr[(length // 2) - 1] + arr[length // 2]) / 2.0          res.append(median)      return res  if __name__ == '__main__':     arr = [5, 15, 1, 3, 2, 8]     res = getMedian(arr)          print(" ".join(f"{median:.2f}" for median in res)) 
C#
// C# program to find Median from Running Data Stream // Using Insertion Sort using System; using System.Collections.Generic;  class GfG {          // Function to print median of stream of integers     static List<double> getMedian(int[] arr) {         List<double> res = new List<double>();         res.Add(arr[0]);          for (int i = 1; i < arr.Length; i++) {             int j = i - 1;             int num = arr[i];              // shift elements to right to create space to insert             // the current element at its correct position             while (j >= 0 && arr[j] > num) {                 arr[j + 1] = arr[j];                 j--;             }             arr[j + 1] = num;              int len = i + 1;             double median;              // If odd number of integers are read from stream             // then middle element in sorted order is median             // else average of middle elements is median             if (len % 2 != 0) {                 median = arr[len / 2];             }             else {                 median = (arr[(len / 2) - 1] + arr[len / 2]) / 2.0;             }              res.Add(median);         }          return res;     }      static void Main() {         int[] arr = { 5, 15, 1, 3, 2, 8 };         List<double> res = getMedian(arr);                  for (int i = 0; i < res.Count; i++)             Console.Write(res[i].ToString("0.00") + " ");              } } 
JavaScript
// JavaScript program to find Median from Running Data Stream // Using Insertion Sort  function getMedian(arr) {     let res = [];      res.push(arr[0]);      for (let i = 1; i < arr.length; i++) {         let j = i - 1;         let num = arr[i];          // shift elements to right to create space to insert         // the current element at its correct position         while (j >= 0 && arr[j] > num) {             arr[j + 1] = arr[j];             j--;         }         arr[j + 1] = num;          let len = i + 1;         let median;          // If odd number of integers are read from stream         // then middle element in sorted order is median         // else average of middle elements is median         if (len % 2 !== 0) {             median = arr[Math.floor(len / 2)];         } else {             median = (arr[len / 2 - 1] + arr[len / 2]) / 2.0;         }          res.push(median);     }      return res; }  // Driver Code let arr = [5, 15, 1, 3, 2, 8]; let res = getMedian(arr);  console.log(res.map(median => median.toFixed(2)).join(" ")); 

Output
5.00 10.00 5.00 4.00 3.00 4.00 

Time Complexity: O(n2), since insertion sort takes O(n2) time to sort n elements. Perhaps we can use binary search on insertion sort to find the location of the next element in O(log n) time. Yet, we can’t do data movement in O(log n) time. No matter how efficient the implementation is, it takes polynomial time in case of insertion sort.
Auxiliary Space: O(1)

[Expected Approach] Using Heaps

The median of an array occurs at the center of sorted array, so the idea is to store the current elements in two nearly equal parts. A max heap (left half) stores the smaller elements, ensuring the largest among them is at the top, while a min heap (right half) stores the larger elements, keeping the smallest at the top.

For each new element:

  1. It is first added to the max heap.
  2. The max heap’s top element is moved to the min heap to maintain order.
  3. If the min heap has more elements than the max heap, its top element is moved back to ensure balance.

This keeps both halves nearly equal in size, differing by at most one element. If the heaps are balanced, the median is the average of their root values; otherwise, it is the root of the heap with more elements.

C++
// C++ program to find Median from Running Data Stream // Using Heaps #include <iostream> #include <vector> #include <queue> #include <iomanip> using namespace std;  // Function to find the median of a stream of data vector<double> getMedian(vector<int> &arr) {          // Max heap to store the smaller half of numbers     priority_queue<int> leftMaxHeap;          // Min heap to store the greater half of numbers     priority_queue<int, vector<int>, greater<int>> rightMinHeap;          vector<double> res;        for (int i = 0; i < arr.size(); i++) {         // Insert new element into max heap         leftMaxHeap.push(arr[i]);                  // Move the top of max heap to min heap to maintain order         int temp = leftMaxHeap.top();         leftMaxHeap.pop();         rightMinHeap.push(temp);                // Balance heaps if min heap has more elements         if (rightMinHeap.size() > leftMaxHeap.size()) {             temp = rightMinHeap.top();             rightMinHeap.pop();             leftMaxHeap.push(temp);         }                  // Compute median based on heap sizes         double median;         if (leftMaxHeap.size() != rightMinHeap.size())             median = leftMaxHeap.top();         else             median = (double)(leftMaxHeap.top() + rightMinHeap.top()) / 2;                  res.push_back(median);     }          return res; }   int main() {     vector<int> arr = {5, 15, 1, 3, 2, 8};     vector<double> res = getMedian(arr);     cout << fixed << setprecision(2);          for (double median: res)          cout << median << " ";     return 0; } 
Java
// Java program to find Median from Running Data Stream // Using Heaps  import java.util.PriorityQueue; import java.util.ArrayList;  class GfG {          // Function to find the median of a stream of data     static ArrayList<Double> getMedian(int[] arr) {                  // Max heap to store the smaller half of numbers         PriorityQueue<Integer> leftMaxHeap = new PriorityQueue<>((a, b) -> b - a);                  // Min heap to store the greater half of numbers         PriorityQueue<Integer> rightMinHeap = new PriorityQueue<>();                  ArrayList<Double> res = new ArrayList<>();                for (int i = 0; i < arr.length; i++) {                          // Insert new element into max heap             leftMaxHeap.add(arr[i]);                          // Move the top of max heap to min heap to maintain order             int temp = leftMaxHeap.poll();             rightMinHeap.add(temp);                        // Balance heaps if min heap has more elements             if (rightMinHeap.size() > leftMaxHeap.size()) {                 temp = rightMinHeap.poll();                 leftMaxHeap.add(temp);             }                          // Compute median based on heap sizes             double median;             if (leftMaxHeap.size() != rightMinHeap.size())                 median = leftMaxHeap.peek();             else                 median = (leftMaxHeap.peek() + rightMinHeap.peek()) / 2.0;                          res.add(median);         }                  return res;     }      public static void main(String[] args) {         int[] arr = {5, 15, 1, 3, 2, 8};         ArrayList<Double> res = getMedian(arr);         System.out.printf("%.2f", res.get(0));                  for (int i = 1; i < res.size(); i++) {             System.out.printf(" %.2f", res.get(i));         }     } } 
Python
# Python program to find Median from Running Data Stream # Using Heaps  import heapq  # Function to find the median of a stream of data def getMedian(arr):          # Max heap to store the smaller half of numbers     leftMaxHeap = []          # Min heap to store the greater half of numbers     rightMinHeap = []          res = []        for num in arr:         # Insert new element into max heap (negating for max behavior)         heapq.heappush(leftMaxHeap, -num)                  # Move the top of max heap to min heap to maintain order         temp = -heapq.heappop(leftMaxHeap)         heapq.heappush(rightMinHeap, temp)                # Balance heaps if min heap has more elements         if len(rightMinHeap) > len(leftMaxHeap):             temp = heapq.heappop(rightMinHeap)             heapq.heappush(leftMaxHeap, -temp)                  # Compute median based on heap sizes         if len(leftMaxHeap) != len(rightMinHeap):             median = -leftMaxHeap[0]         else:             median = (-leftMaxHeap[0] + rightMinHeap[0]) / 2.0                  res.append(median)          return res   if __name__ == "__main__":     arr = [5, 15, 1, 3, 2, 8]     res = getMedian(arr)          print(" ".join(f"{median:.2f}" for median in res)) 
C#
// C# program to find Median from Running Data Stream // Using Heaps using System; using System.Collections.Generic; using System.Linq;  class GfG {     static void insert(SortedDictionary<int, int> heap, int num, ref int size) {         if (heap.ContainsKey(num))             heap[num]++;         else             heap[num] = 1;         size++;     }      static void remove(SortedDictionary<int, int> heap, int num, ref int size) {         if (heap[num] == 1)             heap.Remove(num);         else             heap[num]--;         size--;     }      static int getMax(SortedDictionary<int, int> heap) => heap.First().Key;     static int getMin(SortedDictionary<int, int> heap) => heap.First().Key;          static List<double> getMedian(int[] arr) {                  // Max heap for the smaller half (left side)         SortedDictionary<int, int> leftMaxHeap = new SortedDictionary             <int, int>(Comparer<int>.Create((a, b) => b.CompareTo(a)));         int leftSize = 0;          // Min heap for the greater half (right side)         SortedDictionary<int, int> rightMinHeap = new SortedDictionary                             <int, int>();         int rightSize = 0;          List<double> res = new List<double>();          foreach (int num in arr) {                          // Insert into max heap             insert(leftMaxHeap, num, ref leftSize);              // Move top from max heap to min heap             int temp = getMax(leftMaxHeap);             remove(leftMaxHeap, temp, ref leftSize);             insert(rightMinHeap, temp, ref rightSize);              // Balance the heaps             if (rightSize > leftSize) {                 temp = getMin(rightMinHeap);                 remove(rightMinHeap, temp, ref rightSize);                 insert(leftMaxHeap, temp, ref leftSize);             }              // Compute median             double median;             if (leftSize != rightSize)                 median = getMax(leftMaxHeap);             else                 median = (getMax(leftMaxHeap) + getMin(rightMinHeap)) / 2.0;              res.Add(median);         }          return res;     }      static void Main() {         int[] arr = {5, 15, 1, 3, 2, 8};         List<double> res = getMedian(arr);                  foreach (double median in res)             Console.Write(median.ToString("F2") + " ");     } } 
JavaScript
//Driver Code Starts{ // JavaScript program to find Median from Running Data Stream // Using Heaps  class Heap {     constructor(compare) {         this.data = [];         this.compare = compare;     }      push(val) {         this.data.push(val);         this._heapifyUp();     }      pop() {         if (this.data.length === 0) return null;         if (this.data.length === 1) return this.data.pop();          const top = this.data[0];         this.data[0] = this.data.pop();         this._heapifyDown();         return top;     }      peek() {         return this.data[0] || null;     }      size() {         return this.data.length;     }      _heapifyUp() {         let index = this.data.length - 1;         while (index > 0) {             let parentIndex = Math.floor((index - 1) / 2);             if (this.compare(this.data[parentIndex], this.data[index])) break;                          [this.data[parentIndex], this.data[index]] = [this.data[index], this.data[parentIndex]];             index = parentIndex;         }     }      _heapifyDown() {         let index = 0;         const length = this.data.length;          while (true) {             let leftChildIdx = 2 * index + 1;             let rightChildIdx = 2 * index + 2;             let swapIdx = index;              if (leftChildIdx < length && !this.compare(this.data[swapIdx], this.data[leftChildIdx])) {                 swapIdx = leftChildIdx;             }             if (rightChildIdx < length && !this.compare(this.data[swapIdx], this.data[rightChildIdx])) {                 swapIdx = rightChildIdx;             }             if (swapIdx === index) break;              [this.data[index], this.data[swapIdx]] = [this.data[swapIdx], this.data[index]];             index = swapIdx;         }     } }  // MaxHeap (Stores smaller half of numbers) class MaxHeap extends Heap {     constructor() {         super((a, b) => a > b); // Max heap uses > comparator     } }  // MinHeap (Stores greater half of numbers) class MinHeap extends Heap {     constructor() {         super((a, b) => a < b); // Min heap uses < comparator     } } //Driver Code Ends }  // Function to find the median of a stream of data function getMedian(arr) {          // Max heap for left side     let leftMaxHeap = new MaxHeap();     // Min heap for right side     let rightMinHeap = new MinHeap();           let res = [];      for (let num of arr) {         // Insert into max heap         leftMaxHeap.push(num);          // Balance heaps by moving element to min heap         rightMinHeap.push(leftMaxHeap.pop());          // Ensure left heap has more or equal elements         if (rightMinHeap.size() > leftMaxHeap.size()) {             leftMaxHeap.push(rightMinHeap.pop());         }          // Compute median based on heap sizes         let median;         if (leftMaxHeap.size() !== rightMinHeap.size())             median = leftMaxHeap.peek();         else             median = (leftMaxHeap.peek() + rightMinHeap.peek()) / 2.0;                  res.push(median);     }      return res; }  // Driver Code let arr = [5, 15, 1, 3, 2, 8]; let res = getMedian(arr);  console.log(res.map(median => median.toFixed(2)).join(" ")); 

Output
5.00 10.00 5.00 4.00 3.00 4.00 

Time Complexity: O(n * log n), All the operations within the loop (push, pop) take O(log n) time in the worst case for a heap of size n.
Auxiliary Space: O(n)




Next Article
Largest triplet product in a stream
author
kartik
Improve
Article Tags :
  • DSA
  • Heap
  • Mathematical
  • Adobe
  • Amazon
  • Apple
  • array-stream
  • Belzabar
  • Facebook
  • FactSet
  • Google
  • Hike
  • MAQ Software
  • median-finding
  • Microsoft
  • Morgan Stanley
  • Ola Cabs
  • Oracle
  • SAP Labs
  • statistical-algorithms
  • Yahoo
Practice Tags :
  • Adobe
  • Amazon
  • Apple
  • Belzabar
  • Facebook
  • FactSet
  • Google
  • Hike
  • MAQ Software
  • Microsoft
  • Morgan Stanley
  • Ola Cabs
  • Oracle
  • SAP Labs
  • Yahoo
  • Heap
  • Mathematical

Similar Reads

  • Heap Data Structure
    A Heap is a complete binary tree data structure that satisfies the heap property: for every node, the value of its children is greater than or equal to its own value. Heaps are usually used to implement priority queues, where the smallest (or largest) element is always at the root of the tree. Basic
    2 min read
  • Introduction to Heap - Data Structure and Algorithm Tutorials
    A Heap is a special Tree-based Data Structure that has the following properties. It is a Complete Binary Tree.It either follows max heap or min heap property.Max-Heap: The value of the root node must be the greatest among all its descendant nodes and the same thing must be done for its left and righ
    15+ min read
  • Binary Heap
    A Binary Heap is a complete binary tree that stores data efficiently, allowing quick access to the maximum or minimum element, depending on the type of heap. It can either be a Min Heap or a Max Heap. In a Min Heap, the key at the root must be the smallest among all the keys in the heap, and this pr
    13 min read
  • Advantages and Disadvantages of Heap
    Advantages of Heap Data StructureTime Efficient: Heaps have an average time complexity of O(log n) for inserting and deleting elements, making them efficient for large datasets. We can convert any array to a heap in O(n) time. The most important thing is, we can get the min or max in O(1) timeSpace
    2 min read
  • Time Complexity of building a heap
    Consider the following algorithm for building a Heap of an input array A. A quick look over the above implementation suggests that the running time is [Tex]O(n * lg(n)) [/Tex] since each call to Heapify costs [Tex]O(lg(n)) [/Tex]and Build-Heap makes [Tex]O(n) [/Tex]such calls. This upper bound, thou
    2 min read
  • Applications of Heap Data Structure
    Heap Data Structure is generally taught with Heapsort. Heapsort algorithm has limited uses because Quicksort is better in practice. Nevertheless, the Heap data structure itself is enormously used. Priority Queues: Heaps are commonly used to implement priority queues, where elements with higher prior
    2 min read
  • Comparison between Heap and Tree
    What is Heap? A Heap is a special Tree-based data structure in which the tree is a complete binary tree. Types of Heap Data Structure: Generally, Heaps can be of two types: Max-Heap: In a Max-Heap the key present at the root node must be greatest among the keys present at all of its children. The sa
    3 min read
  • When building a Heap, is the structure of Heap unique?
    What is Heap? A heap is a tree based data structure where the tree is a complete binary tree that maintains the property that either the children of a node are less than itself (max heap) or the children are greater than the node (min heap). Properties of Heap: Structural Property: This property sta
    4 min read
  • Some other type of Heap

    • Binomial Heap
      The main application of Binary Heap is to implement a priority queue. Binomial Heap is an extension of Binary Heap that provides faster union or merge operation with other operations provided by Binary Heap.  A Binomial Heap is a collection of Binomial Trees What is a Binomial Tree?  A Binomial Tree
      15+ min read

    • Fibonacci Heap | Set 1 (Introduction)
      INTRODUCTION:A Fibonacci heap is a data structure used for implementing priority queues. It is a type of heap data structure, but with several improvements over the traditional binary heap and binomial heap data structures.The key advantage of a Fibonacci heap over other heap data structures is its
      5 min read

    • Leftist Tree / Leftist Heap
      INTRODUCTION:A leftist tree, also known as a leftist heap, is a type of binary heap data structure used for implementing priority queues. Like other heap data structures, it is a complete binary tree, meaning that all levels are fully filled except possibly the last level, which is filled from left
      15+ min read

    • K-ary Heap
      Prerequisite - Binary Heap K-ary heaps are a generalization of binary heap(K=2) in which each node have K children instead of 2. Just like binary heap, it follows two properties: Nearly complete binary tree, with all levels having maximum number of nodes except the last, which is filled in left to r
      15 min read

    Easy problems on Heap

    • Check if a given Binary Tree is a Heap
      Given a binary tree, check if it has heap property or not, Binary tree needs to fulfil the following two conditions for being a heap: It should be a complete tree (i.e. Every level of the tree, except possibly the last, is completely filled, and all nodes are as far left as possible.).Every node’s v
      15+ min read

    • How to check if a given array represents a Binary Heap?
      Given an array, how to check if the given array represents a Binary Max-Heap.Examples: Input: arr[] = {90, 15, 10, 7, 12, 2} Output: True The given array represents below tree 90 / \ 15 10 / \ / 7 12 2 The tree follows max-heap property as every node is greater than all of its descendants. Input: ar
      11 min read

    • Iterative HeapSort
      HeapSort is a comparison-based sorting technique where we first build Max Heap and then swap the root element with the last element (size times) and maintains the heap property each time to finally make it sorted. Examples: Input : 10 20 15 17 9 21 Output : 9 10 15 17 20 21 Input: 12 11 13 5 6 7 15
      11 min read

    • Find k largest elements in an array
      Given an array arr[] and an integer k, the task is to find k largest elements in the given array. Elements in the output array should be in decreasing order. Examples: Input: [1, 23, 12, 9, 30, 2, 50], k = 3Output: [50, 30, 23] Input: [11, 5, 12, 9, 44, 17, 2], k = 2Output: [44, 17] Table of Content
      15+ min read

    • K’th Smallest Element in Unsorted Array
      Given an array arr[] of N distinct elements and a number K, where K is smaller than the size of the array. Find the K'th smallest element in the given array. Examples: Input: arr[] = {7, 10, 4, 3, 20, 15}, K = 3 Output: 7 Input: arr[] = {7, 10, 4, 3, 20, 15}, K = 4 Output: 10 Table of Content [Naive
      15+ min read

    • Height of a complete binary tree (or Heap) with N nodes
      Consider a Binary Heap of size N. We need to find the height of it. Examples: Input : N = 6 Output : 2 () / \ () () / \ / () () () Input : N = 9 Output : 3 () / \ () () / \ / \ () () () () / \ () ()Recommended PracticeHeight of HeapTry It! Let the size of the heap be N and the height be h. If we tak
      3 min read

    • Heap Sort for decreasing order using min heap
      Given an array of elements, sort the array in decreasing order using min heap. Examples: Input : arr[] = {5, 3, 10, 1}Output : arr[] = {10, 5, 3, 1}Input : arr[] = {1, 50, 100, 25}Output : arr[] = {100, 50, 25, 1} Prerequisite: Heap sort using min heap. Using Min Heap Implementation - O(n Log n) Tim
      11 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