DefuseBomb.cs

CSharp · Console

/Program.cs

using System;
					
public class Program
{
	public static int[] DefuseBomb(int[] arr, int k) {
		int N = arr.Length;
        var res = new int[N];
        if (k == 0) {
            return res;
        }

        for (int i = 0; i < N; i++)
        {
            if (k > 0) {
                for (int j = i + 1; j < i + k + 1; j++) {
                    res[i] += arr[j % N];
                }
            }
            else 
            {
                for (int j = i - Math.Abs(k); j < i; j++) {
                    res[i] += arr[(j + N) % N];
                }
            }
        }

        return res;
    }
	
	public static void Main()
	{
		Console.WriteLine("UniLecs");
		
		// tests
		Console.WriteLine(string.Join(", ", DefuseBomb(new int[] { 5, 7, 1, 4 }, 3))); // [ 12, 10, 16, 13 ]
		Console.WriteLine(string.Join(", ", DefuseBomb(new int[] { 2, 4, 9, 3 }, -2))); // [ 12, 5, 6, 13 ]
	}
}
;