{{#items}}

{{text}}

{{/items}} {{ctaText}}

{{#items}} {{/items}}

showing results for - "median in a stream of integers java"

Andrés

07 Sep 2019

```
1class MedianOfIntegerStream {
2
3 private Queue<Integer> minHeap, maxHeap;
4
5 MedianOfIntegerStream() {
6 minHeap = new PriorityQueue<>();
7 maxHeap = new PriorityQueue<>(Comparator.reverseOrder());
8 }
9
10 void add(int num) {
11 if (!minHeap.isEmpty() && num < minHeap.peek()) {
12 maxHeap.offer(num);
13 if (maxHeap.size() > minHeap.size() + 1) {
14 minHeap.offer(maxHeap.poll());
15 }
16 } else {
17 minHeap.offer(num);
18 if (minHeap.size() > maxHeap.size() + 1) {
19 maxHeap.offer(minHeap.poll());
20 }
21 }
22 }
23
24 double getMedian() {
25 int median;
26 if (minHeap.size() < maxHeap.size()) {
27 median = maxHeap.peek();
28 } else if (minHeap.size() > maxHeap.size()) {
29 median = minHeap.peek();
30 } else {
31 median = (minHeap.peek() + maxHeap.peek()) / 2;
32 }
33 return median;
34 }
35}
```

similar questions

program to find prime factors of a number in javafind highest and lowest of five integers using java loopssmallest positive integer not in array javajava program to calculate average of n numbersfind max in stream javadifference between greatest and smallest number in javacalculate prime factors of a number javajava program to find largest of three numbers using nested ifjava int stream minprint consecutive numbers in javajava program to find the largest in three numbers using nested ifjava program to find average of n numbersgreatest of three in javahow to find prime and composite numbers in java on an arrayjava find the greatest common divisor of two positive integers the integers can be large 2c so you need to find a clever solution maximum difference in array javafind the smallest length of a contiguous subarray of which the sum is greater than or equal to specified value in javajava program to calculate average of 3 numbersfind average of numbers in array javaqueries leading to this page

median in stream of integershow median can be find using two heapsfind the median in a stream of integersfind the median for a stream of integers median of stream of running integersfind a median in a stream of integersobtain median from heapfind median from data streammedian of the stream of integerfind running median in a stream of integers median of stream of running median in a stream of integersfind running median in a stream of integersmedian of a stream of integers javarunning median binary searchmedian data streammedian of a int stream in javafind median in a stream of integersmedian of a stream of integersfind median of an array using heapsmedian of data streamrunning medianmedian of stream of integersfind median in a stream of running integers find median from stream of integersmedian in a stream of integers java your answer for

you will get a confirmation link on this - you will have to click that for successful submission of your answer. we require this to keep the website free of spam, bots and unhelpful content

please ensure to add code which is syntactically corrent and executes properly

ask question on maxinterview

you will get a confirmation link on this - you will have to click that for successful submission of your question. we require this to keep the website free of spam, bots and unhelpful content

please be clear, to the point and respectful