Pages

Monday, August 18, 2025

Find the Max in an ArrayList

 import java.util.ArrayList;


public class Classroom {

    public static void main(String[] args) {
       ArrayList<Integer> list = new ArrayList<>();
       list.add(2);
       list.add(5);
       list.add(9);
       list.add(6);
       list.add(8);

      // Find Maximum in an ArrayList - O(1)
        int max = Integer.MIN_VALUE;
        for(int i=0; i<list.size(); i++) {
            // if(max < list.get(i)) {
            //     max = list.get(i);
            // }
            max = Math.max(max, list.get(i));

            System.out.println("Max element = " + max);
        }


    }
}

No comments:

Post a Comment

how to become red on codeforces by Errichto Algorithms | explained simply in a blog

 Yes. I found an available transcript of the video and summarized it simply. The video is “How To Become Red Coder? (codeforces.com)” by Err...