Advertisement
Ligh7_of_H3av3n

09. Largest 3 Numbers

May 20th, 2024
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.86 KB | None | 0 0
  1. package Lekciq;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class LargestThreeNumbers {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.  
  11.         // Read a line of integers separated by space
  12.         String inputLine = scanner.nextLine();
  13.  
  14.         // Split the input line into an array of strings
  15.         String[] numbersAsString = inputLine.split(" ");
  16.  
  17.         // Convert the array of strings to an array of integers
  18.         int[] numbers = Arrays.stream(numbersAsString)
  19.                 .mapToInt(Integer::parseInt)
  20.                 .toArray();
  21.  
  22.         // Sort the array of integers in descending order
  23.         Arrays.sort(numbers);
  24.         for (int i = 0; i < Math.min(numbers.length, 3); i++) {
  25.             System.out.print(numbers[numbers.length - i - 1] + " ");
  26.         }
  27.     }
  28. }
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement