Medium

N candles are placed in a row, some of them are initially lit. For each candle from the 1st to the Nth the following algorithm is applied: if the observed candle is lit then states of this candle and all candles before it are changed to the opposite. Which candles will remain lit after applying the algorithm to all candles in the order they are placed in the line?

Example

  • For a = [1, 1, 1, 1, 1], the output should be
    switchLights(a) = [0, 1, 0, 1, 0].

    Check out the image below for better understanding:

  • For a = [0, 0], the output should be
    switchLights(a) = [0, 0].

    The candles are not initially lit, so their states are not altered by the algorithm.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer a

    Initial situation - array of zeros and ones of length N, 1 means that the corresponding candle is lit.

    Guaranteed constraints:
    2 ≤ a.length ≤ 5000.

  • [output] array.integer

    • Situation after applying the algorithm - array in the same format as input with the same length.

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

// Arrays are already defined with this interface:
// typedef struct arr_##name {
//   int size;
//   type *arr;
// } arr_##name;
//
// arr_##name alloc_arr_##name(int len) {
//   arr_##name a = {len, len > 0 ? malloc(sizeof(type) * len) : NULL};
//   return a;
// }
//
//
arr_integer switchLights(arr_integer a)
{
	for(int i=0;i<a.size;i++)
		if(a.arr[i]!=0)
			for(int j=0;j<=i;j++)
				a.arr[j]++;
	for(int i=0;i<a.size;i++)
		a.arr[i]%=2;

	return a;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Integer to String of Fixed width  (0) 2020.05.08
<Codesignal> Timed Reading  (0) 2020.05.04
<Codesignal> Minimal Number of Coins  (0) 2020.05.04
<Codesignal> Alphabet Subsequence  (0) 2020.05.04
<Codesignal> House of Cats  (0) 2020.05.04

+ Recent posts