Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to return arraylist vertically

I am trying to return an array list vertically but I’m having some issues trying to implement it. Below is my code, and the return results; Output is what I’m trying to get to be vertically

public static List<Integer> predictAnswer(List<Integer> stockData, List<Integer> queries) {
// Write your code here

//Declare list for storing result
List<Integer> result = new ArrayList<Integer>();
//Declare variable for storing index of previous and next smaller stock
int previousNear = -1, nextNear =-1;

for (int query : queries){
    // Reset previousNear and nextNear values
    previousNear = nextNear = -1;
    
    //Iterate and find smaller stock to first day; previousNear reamians -1 if smaller stock not found
    for (int day = query - 1; day>= 0; day--){
        if (stockData.get(query - 1) > stockData.get(day)){
            previousNear = day;
            break;
        }
    }
   //Iterate and find smaller stock to last day; nextNear remains -1 if smaller stock not   found
   for (int day = query; day < stockData.size(); day++) {
       
       if (stockData.get(query -1 ) > stockData.get(day)){
           nextNear = day;
           break;
       }
       
   }
   if (previousNear == -1 && nextNear == -1)
        result.add(-1);
        
    else if (previousNear == -1 && nextNear!= -1)
    result.add(nextNear + 1);
    else if (query - previousNear - 1 > nextNear - query + 1)
        result.add(nextNear + 1);
        else result.add(previousNear + 1);
    

}


return result;
    }

}

My current output is [2, 4, -1]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’d like it to be

2
4
-1

Any help would be appreciated. Thank you in advance

>Solution :

Hi if you mean to display each value from results on new line you can do this :

 public static void display() {
    List<Integer> results = List.of(1, 2, 46);
    for (Integer result : results) {
        System.out.println(result);
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading