Advertisement
medon3

Untitled

Oct 14th, 2022
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. import java.util.Scanner;
  5.  
  6. public class AloneNumbers {
  7.  
  8. public static void main(String[] args) {
  9. Scanner sc = new Scanner(System.in);
  10.  
  11. String[] numbers = sc.nextLine().split(", ");
  12. int[] line = new int[numbers.length];
  13. int n = sc.nextInt();
  14. List<Integer> newN = new ArrayList<>();
  15.  
  16. for (int i = 0; i < numbers.length; i++) {
  17. line[i] = Integer.parseInt(numbers[i]);
  18. }
  19.  
  20. if (line.length <= 2) {
  21. for (int element : line) {
  22. newN.add(element);
  23. }
  24.  
  25. System.out.println(newN);
  26. } else {
  27.  
  28. newN.add(line[0]);
  29.  
  30. for (int i = 1; i < line.length - 1; i++) {
  31. if (line[i] == n && line[i] != line[i + 1] && line[i] != line[i - 1]) {
  32. if (line[i + 1] >= line[i - 1]) {
  33. newN.add(line[i + 1]);
  34. } else if (line[i - 1] >= line[i + 1]) {
  35. newN.add(line[i - 1]);
  36. }
  37. } else {
  38. newN.add(line[i]);
  39. }
  40. }
  41.  
  42. newN.add(line[line.length - 1]);
  43. System.out.println(newN);
  44. }
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement