import java.util.*;
import java.lang.*;
import java.io.*;

// The main method must be in a class named "Main".
class Main {
    public static void main(String[] args) {
       Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int k=sc.nextInt();
        List<Integer> list1=new ArrayList<>();
        for(int i=0;i<n;i++){
            int value=sc.nextInt();
            list1.add(value);
        }

        Collections.reverse(list1.subList(0,n-k));
        Collections.reverse(list1.subList(n-k,n));
        Collections.reverse(list1);

        for (int val : list1) {
            System.out.println(val);
        }
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: