Advertisement
jasonpogi1669

Sort First K elements of Array in Java

Aug 7th, 2022 (edited)
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.46 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Test {
  5.     public static void main(String[] args) {
  6.         Scanner sc = new Scanner(System.in);
  7.        
  8.         int k = sc.nextInt();
  9.         sc.close();
  10.        
  11.         int[] a = new int[] {9, 1, 7, 6, 5, 6, 8, 9};
  12.        
  13.         if (k > a.length) {
  14.             System.out.println("Invalid");
  15.             return;
  16.         }
  17.        
  18.         //sort first k numbers
  19.         Arrays.sort(a, 0, k);
  20.         for (int x : a) {
  21.             System.out.print(x+ " ");
  22.         }
  23.        
  24.         System.out.println();
  25.     }
  26. }
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement