CuBG

Untitled

Feb 25th, 2021
657
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 BombNumbers_05 {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         List<Integer> listOfNumbers = Arrays.stream(scanner.nextLine().split("\\s+"))
  11.                 .map(Integer::parseInt)
  12.                 .collect(Collectors.toList());
  13.  
  14.         String[] bombAndPower = scanner.nextLine().split("\\s+");
  15.  
  16.         int bombNumber = Integer.parseInt(bombAndPower[0]);
  17.         int power = Integer.parseInt(bombAndPower[1]);
  18.         int indexOfBomb = getBombIndex(listOfNumbers, bombNumber);
  19.  
  20.         while (listOfNumbers.contains(bombNumber)) {
  21.             getLeftSideExplosion(listOfNumbers, power, indexOfBomb);
  22.             indexOfBomb = getBombIndex(listOfNumbers, bombNumber);
  23.             getRightSideExplosion(listOfNumbers, power, indexOfBomb);
  24.             indexOfBomb = getBombIndex(listOfNumbers, bombNumber);
  25.         }
  26.         getSumOfRemainingNumbers(listOfNumbers);
  27.     }
  28.  
  29.     private static int getBombIndex(List<Integer> listOfNumbers, int bNumber) {
  30.         int bombIndex = 0;
  31.         for (Integer index : listOfNumbers) {
  32.             if (index == bNumber) {
  33.                 bombIndex = listOfNumbers.indexOf(index);
  34.                 break;
  35.             }
  36.         }
  37.         return bombIndex;
  38.     }
  39.  
  40.  
  41.     private static void getLeftSideExplosion(List<Integer> listOfNumbers, int power, int indexOfBomb) {
  42.         for (int i = indexOfBomb - 1; i >= indexOfBomb - power; i--) {
  43.             if (i < 0) {
  44.                 break;
  45.             }
  46.             listOfNumbers.remove(i);
  47.         }
  48.     }
  49.  
  50.     private static void getRightSideExplosion(List<Integer> listOfNumbers, int power, int indexOfBomb) {
  51.         for (int i = 0; i < power; i++) {
  52.             if (indexOfBomb + 1 == listOfNumbers.size()) {
  53.                 break;
  54.             }
  55.             listOfNumbers.remove(indexOfBomb + 1);
  56.         }
  57.         listOfNumbers.remove(indexOfBomb);
  58.     }
  59.  
  60.     private static void getSumOfRemainingNumbers(List<Integer> listOfNumbers) {
  61.         int sum = 0;
  62.         for (Integer num : listOfNumbers) {
  63.             sum += num;
  64.         }
  65.         System.out.println(sum);
  66.     }
  67. }
  68.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×