desislava_topuzakova

05. Bomb Numbers

Oct 16th, 2021
791
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Arrays;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. import java.util.stream.Collectors;
  5.  
  6. public class BombNumber {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         List<String> elements = Arrays.stream(scanner.nextLine()
  11.                 .split("\\s+"))
  12.                 .collect(Collectors.toList());
  13.  
  14.         String[] data = scanner.nextLine().split("\\s+");
  15.         String bombNumber = data[0];
  16.         int power = Integer.parseInt(data[1]);
  17.         int sum = 0;
  18.  
  19.             while (elements.contains(bombNumber)){
  20.                 int elementIndex = elements.indexOf(bombNumber);
  21.  
  22.                 int left = Math.max(0, elementIndex - power);
  23.                 int right = Math.min( elementIndex + power, elements.size() - 1);
  24.  
  25.                 for (int i = right; i >= left; i--) {
  26.                     elements.remove(i);
  27.  
  28.                 }
  29.             }
  30.         System.out.println(elements.stream().mapToInt(Integer::parseInt).sum());
  31.     }
  32. }
RAW Paste Data