Answered step by step
Verified Expert Solution
Question
1 Approved Answer
can someone please explain why my Java code is not printing the value shown in the picture. This is my code:import java.util.Collections; import java.util.PriorityQueue; import
can someone please explain why my Java code is not printing the value shown in the picture. This is my code:import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Scanner;
public class Main
static Scanner in;
Method to calculate med of stream
public static void printMedianint n
max heap to store the smaller half elements
PriorityQueuesmaller new PriorityQueue
CollectionsreverseOrder;
minheap to store the greater half elements
PriorityQueue greater new PriorityQueue;
int data innextInt; first input
smaller.adddata;
double med data; first number itself a median
System.out.printlnmed;
forint i; i greater.size
ifx med
greater.addsmallerremove;
smaller.addx;
else
greater.addx;
med doublesmallerpeekgreater.peek;
Case both heaps are balanced
else if smallersize greater.size
if x med
smaller.addx;
med doublesmallerpeek;
else
greater.addx;
med doublegreaterpeek;
Case the right side heap has more elements
else if smallersize greater.size
if x med
smaller.addgreaterremove;
greater.addx;
else
greater.addx;
med doublesmallerpeekgreater.peek;
System.out.printlnmed;
Driver code
public static void mainString args
TODO Autogenerated method stub
in new ScannerSystemin; Creates a Scanner object
int n innextInt;
printMediann;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started