by CodeChum Admin
Did you know that you can also reverse lists and group them according to your liking with proper code? Let's try it to believe it.
Instructions:
Input
The first line contains an odd positive integer.
The next lines contains an integer.
5
1
3
4
5
2
Output
A line containing a list.
[2,5]-[4]-[3,1]
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int size = in.nextInt();
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < size; i++) {
list.add(in.nextInt());
}
Collections.reverse(list);
System.out.print(list.subList(0, list.size() / 2) + "-");
System.out.print(list.subList(list.size() / 2, list.size() / 2 + 1) + "-");
System.out.println(list.subList(list.size() / 2 + 1, list.size()));
}
}
Comments
Leave a comment