Median Of Stream Of Running Integers in Java10 Sept 2024 | 12 min read An integer array is given to us. Compute the median of the elements traversed so far in the input array. For the sake of simplicity, assume that there are no duplicates. Example: Input int arr[] = {17, 11, 15, 13, 10, 12, 18, 19, 1, 16, 14, 20}; Output: {17, 14, 15, 14, 13, 12, 13, 14, 13, 14, 14, 14.5} Explanation: We start reading the array from left to right. The first element traversed is 17. Hence, the median is 17. It is because only one element is traversed. After that, element 11 is traversed. Now, at this point, we have traversed two elements 17 and 11. Thus, the median becomes (11 + 17) / 2 = 14. After that, element 15 is traversed. Now we have a total of 3 elements traversed, 17, 11, and 15, and the median of an odd number of elements is the middle element when the elements are arranged in ascending order. Thus, the median of those three elements is {11, 15, 17} => {15}. Now, element 13 comes into the picture, and a total of 4 elements are traversed, and the median of the even number of elements is the average of the two middle elements when the elements are sorted. Thus, the current median is: {11, 13, 15, 17} => (13 + 15) / 2 = 14. Similarly, we can compute the other medians too. Approach: Using Insertion SortIt is evident from the above example that for computing the median. It is required to sort the integers that are traversed till now. Therefore, we need a sorting technique to sort the elements. We will be using insertion sort to sort the elements. FileName: RunningIntegerStream.java Output: Median after reading element 17 is: 17
Median after reading { 11 17 } elements is 14.0
Median after reading { 11 15 17 } elements is 15.0
Median after reading { 11 13 15 17 } elements is 14.0
Median after reading { 10 11 13 15 17 } elements is 13.0
Median after reading { 10 11 12 13 15 17 } elements is 12.5
Median after reading { 10 11 12 13 15 17 18 } elements is 13.0
Median after reading { 10 11 12 13 15 17 18 19 } elements is 14.0
Median after reading { 1 10 11 12 13 15 17 18 19 } elements is 13.0
Median after reading { 1 10 11 12 13 15 16 17 18 19 } elements is 14.0
Median after reading { 1 10 11 12 13 14 15 16 17 18 19 } elements is 14.0
Median after reading { 1 10 11 12 13 14 15 16 17 18 19 20 } elements is 14.5
Complexity Analysis: The program uses insertion sort technique. Thus, the time complexity of the program is O(n2). The program is not using any data structure. Hence, the space complexity of the program is constant, i.e., O(1). Note: One may argue why the insertion sort technique has been used in the program. One can also use other sorting techniques too, like selection sort, merge sort etc. The reason behind this is that at any given instance of insertion sorting, say doing sorting of the kth element, the first k elements of the input array are sorted. The insertion sort does not depend on the data that are coming next (future data) to sort the data present till that point. In other words, insertion sort does data sorting so far while the next element is inserted. It is the important aspect of the insertion sort that makes this algorithm an online algorithm.Approach: Using AVL TreeUsing the AVL tree, one can find the median of the running integers. One by one, we will be inserting the elements in the AVL tree and will also keep track of the FileName: RunningIntegerStream1.java Output: Median after reading element 17 is: 17
Median after reading { 17 11 } elements is 14.0
Median after reading { 17 11 15 } elements is 15.0
Median after reading { 17 11 15 13 } elements is 14.0
Median after reading { 17 11 15 13 10 } elements is 13.0
Median after reading { 17 11 15 13 10 12 } elements is 12.5
Median after reading { 17 11 15 13 10 12 18 } elements is 13.0
Median after reading { 17 11 15 13 10 12 18 19 } elements is 14.0
Median after reading { 17 11 15 13 10 12 18 19 1 } elements is 13.0
Median after reading { 17 11 15 13 10 12 18 19 1 16 } elements is 14.0
Median after reading { 17 11 15 13 10 12 18 19 1 16 14 } elements is 14.0
Median after reading { 17 11 15 13 10 12 18 19 1 16 14 20 } elements is 14.5
Complexity Analysis: In an AVL tree, the time complexity of inserting an element is O(log(n). Therefore, the overall time complexity of the program is O(n x log(n)). The program allocates memories for the nodes of the AVL tree. Therefore, the space complexity of the program is O(n), where n is the total number of running integers that have been traversed. Approach: Using Min and Max HeapThe concept is for keeping the min heap and max heap for keeping the elements of the lower half and the higher half. The min heap and max heap in Java can be implemented with the help of PriorityQueue. The following steps are required to solve the problem. Algorithm:Step 1: Create two heaps: one for the max heap for maintaining elements of the lower half and another one for the min heap for maintaining elements of the higher half at any given point in time. Step 2: Initialize the value of the median as 0. Step 3: For every currently read element, insert it either into the min heap or max heap and compute the median with the help of the following conditions:
FileName: DisplayLeafBST1.java Output: Median after reading element 17 is: 17
Median after reading { 17 } elements is 14.0
Median after reading { 17 11 } elements is 15.0
Median after reading { 17 11 15 } elements is 14.0
Median after reading { 17 11 15 13 } elements is 13.0
Median after reading { 17 11 15 13 10 } elements is 12.5
Median after reading { 17 11 15 13 10 12 } elements is 13.0
Median after reading { 17 11 15 13 10 12 18 } elements is 14.0
Median after reading { 17 11 15 13 10 12 18 19 } elements is 13.0
Median after reading { 17 11 15 13 10 12 18 19 1 } elements is 14.0
Median after reading { 17 11 15 13 10 12 18 19 1 16 } elements is 14.0
Median after reading { 17 11 15 13 10 12 18 19 1 16 14 } elements is 14.5
Complexity Analysis: The time, as well as space complexity of the program, is the same as the previous program. Next TopicArrow Operator in Java |
Check LinkedLists are Equal
Given two singly linked lists, check if they are equal. Two linked lists are considered equal if: They have the same number of nodes. The data in each corresponding node is the same. Example 1 Input: List1 = 1 -> 2 -> 3 -> null List2 = 1 -> 2 ->...
6 min read
Find all Primes to a Given Natural Number Using the Sieve of Eratosthenes Method
The Sieve of Eratosthenes is one of the most efficient algorithms for identifying all prime numbers up to a given number, the limit. The above-stated procedure is named after the ancient Greek mathematician Eratosthenes, who developed this intelligent technique. It operates on a simple principle: each...
5 min read
Finding the Maximum Points on a Line in Java
In many applications and methods in the fields of mathematics and computer science, lines are important. Finding the most points possible that can fit on a line in a given set of 2D coordinates is a typical issue. Applications for this issue include machine learning, computer...
5 min read
Difference Between Containers and Components in Java
In the world of Java programming, developers often encounter the terms "container" and "component." These terms are fundamental to Java's graphical user interface (GUI) development, and understanding their distinctions is crucial for creating robust and modular applications. In this section, we will explore the key differences...
4 min read
Can we make the main() thread as daemon in Java
? The main() function in Java is where any standalone application should start. The "main" thread, which is by default a non-daemon thread, is in charge of carrying it out. It indicates that until the main() thread and all non-daemon threads have completed executing, the Java...
4 min read
Convert String to Map in Java
Handling key-value-based data is a common requirement in various Java applications. Often, data arrives as Strings or String arrays, and it becomes essential to convert them into Maps for efficient processing. In the same context, Map provide an easy way to access and manipulate data with...
5 min read
Transfer Statements in Java
In Java, transfer statements are a set of keywords that allow you to control the flow of execution within a program. They provide mechanisms for altering the default sequence of control flow in loops and conditional blocks. These statements include break, continue, and return. Let's take...
4 min read
DoubleBuffer flip() methods in Java with Examples
A buffer that holds double data in Java is called a DoubleBuffer. It belongs to the Java.nio package and is a subclass of the Buffer class. Buffers can be made ready for reading data after writing by using the flip() method and vice versa. By first...
3 min read
nth Prime Number Java
A number is prime if it is divisible by 1 and itself. In other words, a prime number is a natural number with exactly two distinct natural number divisors 1 and itself. For example, 2, 3, 5, 7, 11, etc. are the prime numbers. Note that...
5 min read
Shunting Yard Algorithm in Java
The Shunting Yard algorithm is a commonly used algorithm in computer science for converting infix expressions to postfix or prefix expressions. In postfix notation, also known as Reverse Polish Notation (RPN), the operator is placed after the operands, while in prefix notation, also known as Polish...
8 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India