Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- public class Program
- {
- public static void Main()
- {
- // 4 2
- // expected [1, 2, 9] with sum 12;
- List<int> list = new List<int>() { 1, 2, 2, 4, 2, 2, 2, 9 };
- detonateBomb(list, 4, 2);
- // 9 3
- // expected [1, 4] with sum 5;
- list = new List<int>() { 1, 4, 4, 2, 8, 9, 1 };
- detonateBomb(list, 9, 3);
- // 7 1
- // expected [1, 2, 3] with sum 6;
- list = new List<int>() { 1, 7, 7, 1, 2, 3 };
- detonateBomb(list, 7, 1);
- // 2 1
- // expected [1, 1, 1, 1] with sum 4;
- list = new List<int>() { 1, 1, 2, 1, 1, 1, 2, 1, 1, 1 };
- detonateBomb(list, 2, 1);
- }
- public static void detonateBomb(List<int> list, int specialNumber, int power)
- {
- printTheList(list);
- Console.WriteLine("Special number: " + specialNumber + ", power: " + power);
- // 1, 2, 2, 4, 2, 2, 2, 9 - 8.count
- for (int index = 0; index < list.Count; index++)
- {
- if (specialNumber == list[index])
- {
- // define the lower border
- int lowerBorder = index - power;
- if (lowerBorder < 0)
- {
- lowerBorder = 0;
- }
- // define the upper border
- int upperBorder = index + power;
- if (upperBorder >= list.Count)
- {
- upperBorder = list.Count - 1;
- }
- int numberOfElementsToRemove = upperBorder - lowerBorder;
- // remove the elements on the left, detonate the bomb and
- // remove the elements on the right
- // less than or equal to the number, since the last element should be removed inclusive
- for (int i = 0; i <= numberOfElementsToRemove; i++)
- {
- list.RemoveAt(lowerBorder);
- }
- }
- }
- printTheListAndTheSum(list);
- }
- public static void printTheList(List<int> list)
- {
- Console.Write("[");
- for (int i = 0; i < list.Count; i++)
- {
- Console.Write(list[i]);
- if (i < list.Count - 1)
- {
- Console.Write(", ");
- }
- }
- Console.WriteLine("]");
- }
- public static void printTheListAndTheSum(List<int> list)
- {
- printTheList(list);
- int sum = 0;
- for (int i = 0; i < list.Count; i++)
- {
- sum += list[i];
- }
- Console.WriteLine("The sum is " + sum + "\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement