Menu

(Solved) : Computational Challenge Company Massive Text File Loaded Main Memory Working File N Lines Q44005182 . . .

Computational Challenge. The company has a massive text file,which can be loaded into the main memory that you will be workingon. The file has n lines of integer numbers, recording the pricesof a particular stock over the past n days. The integer number onthe kth line, denoted as p(k), represents the price of the stock onthe kth day, 0 ≤ k ≤ n − 1. Let’s assume all the numbers in thefile are DISTINCT. Your boss wants you to find two numbers in andout, 0 ≤ in ≤ out ≤ n − 1, such that p(out) − p(in) = max{p(j) −p(i) | 0 ≤ i ≤ j ≤ n − 1}

That is, you want to find two days to have the stock purchasedand sold such that the profit from trading this stock is maximized.Note that (1) there might be multiple such pairs of in and out, butyou only need to find one of them in the case there are multiplechoices. (2) in and out could even be the same, for example, in thecase that the prices are going down all the way over those ndays.

Your goal is to find an interesting recursion-basedcomputational strategy, whose overall time cost is O(n log n),which is much faster than the trivial idea.

specifications

1. Your program should be named as: BestTrading.java

2. The input and output of your program. The input to yourprogram is a text file, where each line is an integer number. Allthe numbers in the text file are guaranteed to be DISTINCT. Thefile name should be supplied to your program as a command lineparameter. For example, if we supply the file named data.txt thathas the following content 3 9 1 5 Then, the command line you shouldtype would be: $java BestTrading data.txt and the output should be:$[1,2,4] telling us that we should have bought the stock on day 1and sold it on day 2, so that the profit is maximized as 4. Notethat we use the 0-based indexing here.

Java code so far

import java.io.File;
import java.util.Scanner;

public class BestTrading
{
   public static void main(String[]args)throwsException
   {
   File inf = new File(args[0]);
Scanner fin = new Scanner(inf);
int count = 0;
while(fin.hasNext())
{
fin.nextLine();
count++;
}
//putting all the numbers into an array
fin = new Scanner(inf);
int[] array = new int[count];
for(int x = 0; x<count;x++) {
array[x] = Integer.parseInt(fin.nextLine());
}
int length =array.length;
int low,mid,high;
low=array[0];
mid=length/2;
high=length;
   }
}
  

Expert Answer


Answer to Computational Challenge. The company has a massive text file, which can be loaded into the main memory that you will be …

OR