Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The algorithm compares the median of the last m sales prices with the last price. If this median is greater than the last price, the

The algorithm compares the median of the last m sales prices with the last price. If this median is greater than the last price, the algorithm recommends that you sell the stock; if the median is equal to the last price, the algorithm recommends that you hold; and if the median is less than the last prices, the algorithm recommends that you buy.

Input Format

The first line of input consists of two integer n and m, 3 n 5 * 105 and 3 m n.

n indicates the number of prices in the input, and m indicates how many of the most recent values should be considered when calculating the median.

The remaining n lines give a sequence of prices.

Constraints

m will be odd.

Output Format

Starting with the mth price, your program should output, on a line by itself, buy, hold, or sell, based on the comparison of the median of the last m prices and the most recent price.

Note: You will output n - m + 1 values

Sample Input

5 3 12 10 9 9 11 

Sample Output

sell hold buy 

Explanation

For the first decision, the program calculates the median of <12, 10, 9> which is 10, and compares this with the last price 9. Since the median is greater than the last price, the program outputs "sell".

For the second decision, the program calculates the median of <10, 9, 9> which is 9, and compares this with the last price 9. Since the median is equal to the last price, the program outputs "hold".

For the third decision, the program calculates the median of <9, 9, 11> which is 9, and compares this with the last price 11. Since the median is less than the last price, the program outputs "buy".

What I have so far...

Problem is to sort in groups with m elements and find the median within the each group

and comparing to the latestPrice.

import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; import java.util.NoSuchElementException;

interface iMedianQueue > { void enqueue(Object item); Object dequeue(); int size(); void median(int x,int y); } class medianQueue implements iMedianQueue {

private Object[] array; private int size = 0; private int head = 0; // index of the current front item, if one exists private int tail = 0; // index of next item to be added

public medianQueue(int capacity) { array = new Object[capacity]; }

public void enqueue(Object item) { if (size == array.length) { throw new IllegalStateException("Cannot add to full queue"); } array[tail] = item; tail = (tail + 1) % array.length; size++; } public Object dequeue() { if (size == 0) { throw new NoSuchElementException("Cannot remove from empty queue"); } Object item = array[head]; array[head] = null; head = (head + 1) % array.length; size--; return item; } //sort within range given by the index and return the median public int sort(Object[] target, int start, int end){ Object[] findMedian= target; int temp = 0; for(int i=start; i < end; i++){ for(int j=1; j < (end-i); j++){ if((int)findMedian[j-1] > (int)findMedian[j]){ temp = (int)findMedian[j-1]; findMedian[j-1] = (int)findMedian[j]; findMedian[j] = temp; } } } return (int)preSort[((start + end)/2)];// } public int size() { return size; }

public void median(int initial_pos, int group_end){ int latestPrice=(int)array[(initial_pos + group_end)-1];//last numbers //System.out.println("the lastest price is"+ latestPrice); int median = sort(array,initial_pos,group_end); //System.out.println(sort(array,initial_pos,group_end)); if(median == latestPrice) System.out.println("hold"); else if(median>latestPrice) System.out.println("sell"); else System.out.println( "buy"); } }

public class Solution{ public static void main(String []args)throws IOException{ BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

String[] tokens = reader.readLine().split(" "); int n = Integer.valueOf(tokens[0]); // n indicates the number of prices in the input int m = Integer.valueOf(tokens[1]); //most recent tanscations //m will be odd medianQueue q = new medianQueue (n);

for (int i = 0; i < n; i++) { int lastPrice = Integer.valueOf(reader.readLine()); //read in new numbers q.enqueue(lastPrice); } for(int i=0;i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases And Python Programming MySQL MongoDB OOP And Tkinter

Authors: R. PANNEERSELVAM

1st Edition

9357011331, 978-9357011334

More Books

Students also viewed these Databases questions

Question

What is Accounting?

Answered: 1 week ago

Question

Define organisation chart

Answered: 1 week ago

Question

What are the advantages of planning ?

Answered: 1 week ago

Question

Explain the factors that determine the degree of decentralisation

Answered: 1 week ago

Question

Is this investment worthwhile? Why or why not?

Answered: 1 week ago