this is a common question for the heap data structure.
we maintain a max and min heap in the following way - After processing an incoming element, the number of elements in heaps differ utmost by 1 element. When both heaps contain same number of elements, we pick average of heaps root data as effective median. When the heaps are not balanced, we select effective median from the root of heap containing more elements.
example
for 5 -> 5
for 5,15 -> 5 and 15
for 5,15,1 -> 5
---------------------------------------------------------
code
#include<iomanip>
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
int main(void)
{
priority_queue<int> max_heap;
priority_queue< int, vector <int>, greater
<int> > min_heap;
long long i,n,k;
cin>>n;
cin>>k;
max_heap.push(k);
n--;
double a = max_heap.top();
cout<<fixed<<setprecision(1)<<a<<endl;
while(n--)
{
/*inserting*/
cin>>k;
if(k >= max_heap.top())
{
min_heap.push(k);
}
else if(min_heap.empty())
{
min_heap.push(max_heap.top());
max_heap.pop();
max_heap.push(k);
}
else
max_heap.push(k);
/*balancing*/
if(abs(max_heap.size() -
min_heap.size()) > 1)
{
if(max_heap.size() > min_heap.size())
{
min_heap.push(max_heap.top());
max_heap.pop();
}
else
{
max_heap.push(min_heap.top());
min_heap.pop();
}
}
/* finding the median */
if( (min_heap.size() +
max_heap.size())%2 == 0)
{
cout<<min_heap.top()<<"
"<<max_heap.top()<<endl;
}
else if(min_heap.size() >
max_heap.size())
{
double ans =
min_heap.top();
cout<<fixed<<setprecision(1)<<ans<<endl;
}
else
{
double ans =
max_heap.top();
cout<<fixed<<setprecision(1)<<ans<<endl;
}
}
}
---------------------------------------------------------------
this is my code for this!
sample run -
input - //first input the total number of elements 6 //now the elements one by one 12 4 5 3 8 7
output -
12
4 12
5
4 5
5
5 7
-----------------------------------------------------------
thank you!
note - try and do this yourself, will really help you to learn
Given a set of keys, the median key is the key in the "middle". When the...
package hw3; import java.util.LinkedList; /* *********************************************************************** * A simple BST with int keys and no values * * Complete each function below. * Write each function as a separate recursive definition (do not use more than one helper per function). * Depth of root==0. * Height of leaf==0. * Size of empty tree==0. * Height of empty tree=-1. * * TODO: complete the functions in this file. * DO NOT change the Node class. * DO NOT change the name...
52.1 Three primary statistics are used to describe a variable's central tendency, defined as the average behavior of the variable. You can use these statistics, known as the mean, the median, and the mode, to draw meaningful conclusions about a variable's past behavior and to predict its future behaviar An investor is considering a stock with the following returns over the past 12 years: Year Annual Return Year Annual Return 12 10 12 The mean, also known as the arithmetic...
Concurrent Key-Value Database Implement a non-persistent, concurrent key-value database using the Reader/Writers algorithm. - Use a hashmap as the underlying data structure. Inspiration is ok, however the implementation must be yours. The hashmap must be able to grow to fit new elements, but it does not need to reduce its size. -- Linked lists? Positional arrays? All are fine. - Keys and values are strings (char *) - Operations are: get, put Provided files: - Implement the contract set in...
Year Annual Return Year Annual Return 9% 8% 12 14 10 4 4 12 Mean The mean, also known as the arithmetic mean, tells the The mean adds up all the observations and divides by the number of observations, as shown in the following formula value a variable is expected to take rt The mean (or average) return for this stock over the past 12 years is mean, while μ represents a or notational purposes, x-bar represents a However, they...
Add printRang method to BST.java that, given a low key value, and high key value, print all records in a sorted order whose values fall between the two given keys. (Both low key and high key do not have to be a key on the list). BST.java import java.lang.Comparable; /** Binary Search Tree implementation for Dictionary ADT */ class BST<Key extends Comparable<? super Key>, E> implements Dictionary<Key, E> { private BSTNode<Key,E> root; // Root of the BST int nodecount; //...
This lab serves as an intro to Java Interfaces and an Object-Oriented design strategy towards implementing data structures. We will be using Entry.java objects which are key,value pairs, with key being an integer and value being a generic parameter. We will be sorting these Entries according to their key value. The file Entry.java is given to you and does not require any modification. As you should know by now, bucket sort works by placing items into buckets and each bucket...
JAVA 3 LECTURE REVIEW PLEASE NEED ANSWERS ASAP. DUE IN AN HOUR!!! Question 12 points The best-case performance for a shell sort is: --- O(1) O(n2) O(n) O(n log n) Signaler cette question Question 22 points The best-case performance for an array of n items using insertion sort is: --- O(n2) O(n) O(1) there is no best-case Signaler cette question Question 3 2 points A recursive method that processes a chain of linked nodes --- uses the first node in...
Assignment #2: List - Array Implementation Review pages 6-7 in "From Java to C++" notes. Due Friday, February 9th, 2017 @ 11:59PM EST Directions Create a List object. Using the following definition (List.h file is also in the repository for your convenience) for a list, implement the member functions (methods) for the List class and store the implementation in a file called List.cpp. Use an array to implement the list. Write the client code (the main method and other non-class...
in JAVA program.Use top-down design to design and implement a program to ask the user to enter a list of integers, and then display to the user the mean and median of this list. You should first prompt the user to specify the length of the list of integers. For this assignment, your code should create an array of size 10, and then allow the user to specify the number of integers in their list, up to a maximum of...
Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation of the Table ADT. Make sure that you apply the concepts of design by contract (DbC) to your implementation. Once you have fully implemented the table, create a main.c file that implements a testing framework for your table. Your table implementation must ensure that values inserted are unique, and internally sorted within a linked list. table.h #ifndef _TABLE_H #define _TABLE_H //----------------------------------------------------------------------------- // CONSTANTS AND...