Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class Test {
- public static void main(String[] args) {
- ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(9, 1, 7, 10, 1, 5, 4, 6));
- Scanner sc = new Scanner(System.in);
- int k = sc.nextInt();
- sc.close();
- if (k > arr.size()) {
- System.out.println("Invalid");
- return;
- }
- //max element among the first k elements
- int mx = Collections.max(arr.subList(0, k));
- System.out.println(mx);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement