Advertisement
Guest User

Mock Exam3: Alone Numbers

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