Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- public class AloneNumbers {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String[] numbers = sc.nextLine().split(", ");
- int[] line = new int[numbers.length];
- int n = sc.nextInt();
- List<Integer> newN = new ArrayList<>();
- for (int i = 0; i < numbers.length; i++) {
- line[i] = Integer.parseInt(numbers[i]);
- }
- if (line.length <= 2) {
- for (int element : line) {
- newN.add(element);
- }
- System.out.println(newN);
- } else {
- newN.add(line[0]);
- for (int i = 1; i < line.length - 1; i++) {
- if (line[i] == n && line[i] != line[i + 1] && line[i] != line[i - 1]) {
- if (line[i + 1] >= line[i - 1]) {
- newN.add(line[i + 1]);
- } else if (line[i - 1] >= line[i + 1]) {
- newN.add(line[i - 1]);
- }
- } else {
- newN.add(line[i]);
- }
- }
- newN.add(line[line.length - 1]);
- System.out.println(newN);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement