
/*
Online Java - IDE, Code Editor, Compiler

Online Java is a quick and easy tool that helps you to build, compile, test your programs online.
*/

import java.io.*; 
import java.util.*;

public class Main
{
    public static void main(String[] args) {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        
        //According to this problem you can buy the stock at 0 and sell it for a profit.
        // For example the solution to this problem states
        // for stocks    5,2,4,0,1     (4-2)  +  (1-0)   =  3
        // However for this code I have prevented this.
        // Here are some scenarios created to test this problem. All seem to function.
        
        int[] stock = new int[]{1,3,2,8,4,10};  //CORRECT
        //int[] stock = new int[]{5,2,4,0,1};  // CORRECT
        // int[] stock = new int[]{1,1,8,12,15};   // CORRECT
        // int[] stock = new int[]{1,3,2,8,10};  //PASSES
        //int[] stock = new int[]{5,4,8};  //PASSES
        //int[] stock = new int[]{0,4,6,3,2};  //PASSES
        
        int[][] difference = new int[stock.length][20];
        int count=0;
        int temp=0;
        int k;
        boolean profitPossible=false;
       
        int tempStore=0;
        int runningTotal=0;
        int num=20;
        int [] profit = new int[num];
        num=0;
        int max=0;
        int penultimateProfit=0;
        boolean previousProfit=false;
        boolean bypassPenultimate=false;
        
        for (int i=0; i<stock.length;i++)
        {
            System.out.println("\nStocks bought");
            System.out.println(Arrays.toString(stock));
            System.out.println("\n**************************");
            System.out.println("\nStock being evaluated: " + stock[i]);
            System.out.println("**************************");
            
            //System.out.println("current running total:" + runningTotal);
            
            System.out.println(stock[i]);
            count=0;
            runningTotal=0;
            //penultimateProfit=false;
            previousProfit=false;
            bypassPenultimate=false;
            //penultimateProfit=false;
            
            for (int j=i+1; j<stock.length;j++)  // this ensures stock is not compared against itself
            {
                if (j!=stock.length-1)
                {
                    if (j==i)
                {
                    j++;
                }
               
                 // this checks if next stock is greater or higher than examined stock
                 // and also if one after next is lower. These conditions will allow point of sale and rebuy at new value
                 
                 // this is to try and fix circumstance for 1, 3 ,2, 8, 10
                 //currently it is doing 10 - 8 and skipping 10-2
                 // this will try to accomodate for this condition.
                 
                 
                   if (stock[j]>=stock[i]&& stock[j+1]>stock[j] && stock[i]!=0 && j+1==stock.length-1 && !bypassPenultimate) 
                {
                    System.out.println("ENTERRRRRRRRRRRRRRRRRRRRRRRRRRRRRR");
                    profitPossible=true;
                    System.out.println("Next highest stock is:" + stock[j+1]);
                    difference[i][count]=stock[j+1]-stock[j];
                     runningTotal=runningTotal + difference[i][count];
                     
                   System.out.println("this is the profit so far1: " + difference[i][count] + "("+stock[j+1] + "-" + stock[j]+")");
                   profit[num]=runningTotal;
                   previousProfit=true;
                   
                   bypassPenultimate=true;
                    
                }
                 
                 if (stock[j]>=stock[i]&& stock[j+1]<stock[j] && stock[i]!=0 && temp!=j && !bypassPenultimate) 
                {
                    profitPossible=true;
                   
                    System.out.println("Next highest stock is:" + stock[j]);
                    difference[i][count]=stock[j]-stock[i];
                    runningTotal=runningTotal + difference[i][count];
                    System.out.println("this is the profit so far1: " + difference[i][count] + "("+stock[j] + "-" + stock[i]+")");
                   profit[num]=runningTotal;
                   previousProfit=true;
                    // this will check all other occurences from the current point if there is scope to sell and buy
                    //conditions are similar
                    
                    for (temp=j+1;temp<stock.length;temp++) 
                    {
                        // conditions are similar.. if next item is same or greater.. And following stock is lower than previous 
                        if (stock[temp]>=stock[j] && stock[j+1]<stock[j] && !bypassPenultimate)
                        {
                            if (temp+1==stock.length-1)  // this is checking if last stock in list and to avoid any exceptions, it breaks out of loop
                            
                            {
                                tempStore=difference[i][count];
                                difference[i][count]=difference[i][count] + (stock[temp+1]-stock[temp]);
                                
                                if (previousProfit)
                                {
                                    runningTotal=difference[i][count];
                                }
                                if (!previousProfit)
                                {
                                    runningTotal=runningTotal + difference[i][count];
                                }
                                
                                //runningTotal=runningTotal + difference[i][count];
                                System.out.println("this is the profit so far2: " + difference[i][count] + "("+ stock[temp+1] + "-" + stock[temp] +"+"+ tempStore + ")");
                                
                                System.out.println("this is the stock: " + stock[i]);
                                System.out.println("this is the num: " + num);
                                profit[num]=runningTotal;
                                 System.out.println("this is the running Total: " + profit[num]);
                                num++;
                                break;
                                
                            }
                            
                            System.out.println("This is the next highest stock: " + stock[temp] + " after " + stock[temp-1]);
                            tempStore=difference[i][count];
                            difference[i][count]=difference[i][count] + (stock[temp]-stock[temp-1]);
                            System.out.println("this is the profit so far3: " + difference[i][count] + "("+ stock[temp] + "-" + stock[temp-1] +"+"+ tempStore + ")");
                            
                            //positionCompared=temp;
                            
                            if (previousProfit)
                                {
                                    runningTotal=difference[i][count];
                                }
                                if (!previousProfit)
                                {
                                    runningTotal=runningTotal + difference[i][count];
                                }
                            
                            
                            //runningTotal=runningTotal + difference[i][count];
                            profit[num]=runningTotal;
                            //System.
                            System.out.println("this is the running Total: " + profit[num]);
                             System.out.println("this is the stock: " + stock[i]);
                            System.out.println("this is the num: " + num);
                            
                            num++;
                            break;
                        }
                        
                    }
                }
                
                }
                
                // this is now creating a scenario to examine if there has been a buy and resell
                // it also checks scenario such as  2,4,0,1  since the current logic above
                // does not account for a buy and sell as last two stocks
                
                if (stock[j]>stock[j-1] && profitPossible && j==stock.length-1 && i==stock.length-2 && bypassPenultimate)
                {
                    System.out.println("This should be penultimate stock:" + stock[i]);
                    System.out.println("This should be last stock:" + stock[j]);
                    
                    tempStore=difference[i][count];
                    runningTotal = runningTotal + (stock[j]-stock[j-1]);
                    
                    // It is capturing value here
                    penultimateProfit=(stock[j]-stock[j-1]);
                    
                    num++;
                    profit[num]=runningTotal;
                    
                     System.out.println("this is the running Total: " + profit[num]);
                     System.out.println("this is the stock: " + stock[i]);
                                System.out.println("this is the num: " + num);
                    
                    System.out.println("this is the profit so far44: " + runningTotal + "("+stock[j] +"-"+ stock[j-1]+")");
                    profit[num]=runningTotal;
                    System.out.println("************REACH********");
                    break;
                }
                
                // this now covers scenario such as   1 , 1, 8 , 15
                // without this logic, there will be no buy sell and buy
                
                // checks if stock is higher than examined stock. 
                // it will only enter this scenario also if no other profits have been analysed
                // otherwise totals will be incorect.. For instance, it would process  8 => 15 when it
                // would be completed as part of normal logic.
                
                System.out.println("WHY NOT HERE!!!!!!!!!!!");
                System.out.println(stock[j]);
                System.out.println(stock[i]);
                System.out.println("value of j:" + j);
                System.out.println("length:" + (stock.length-1));
                
                 if (stock[j]>stock[i] && !profitPossible && j==stock.length-1 && stock[i]!=0)
                {
                    //System.out.println("Rare instance of all increasing");
                    System.out.println("This stock:" + stock[i]);
                    System.out.println("This should be last stock:" + stock[j]);
                    
                    tempStore=difference[i][count];
                    if (previousProfit)
                                {
                                    runningTotal = runningTotal + (stock[j]-stock[i]);
                                }
                                if (!previousProfit)
                                {
                                    runningTotal=(stock[j]-stock[i]);
                                }
                    
                    //runningTotal = runningTotal + (stock[j]-stock[i]);
                    profit[num]=runningTotal;
                    
                     num++;
                    
                    System.out.println("this is the profit so far55: " + runningTotal + "("+stock[j] +"-"+ stock[i]+")");
                    System.out.println("************REACH AGAIN********");
                    break;
                }
                 
        }
        System.out.println("This is the stock:" + stock[i]);
                    System.out.println("This is all possible routes to make profit:" + difference[i][count]);
                    //profit[num]=runningTotal;
                    System.out.println("VALUE HERE:");
                   //System.out.println(profit[num]);
                    
                    num++;
                    count++;
    }
    
    // this is simply assigning the highest profitable route
    for (int p=0;p<profit.length;p++)
{
    System.out.println("ALL VALUES");
    System.out.println(profit[p]);
    if (profit[p]>max)
    {
        max=profit[p];
    }
    
}

// this gets the profit total if shares are not as such:
//5,2,4,0,1

// this now checks if scenario such as above


if (penultimateProfit!=0 && previousProfit)
{
System.out.println("This is the highest profit possible1: " + (max + penultimateProfit));
System.out.println(max);
System.out.println(penultimateProfit);

}
else
{
    System.out.println("This is the highest profit possible: " + max);
    
}
    
    
}

}