Easy

Given array of integers, remove each kth element from it.

Example

For inputArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and k = 3, the output should be
extractEachKth(inputArray, k) = [1, 2, 4, 5, 7, 8, 10].

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer inputArray

    Guaranteed constraints:
    5 ≤ inputArray.length ≤ 15,
    -20 ≤ inputArray[i] ≤ 20.

  • [input] integer k

    Guaranteed constraints:
    1 ≤ k ≤ 10.

  • [output] array.integer

    • inputArray without elements k - 1, 2k - 1, 3k - 1 etc.

[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 extractEachKth(arr_integer inputArray,int k)
{
	arr_integer returnArray=alloc_arr_integer(inputArray.size-inputArray.size/k);

	for(int i=0;i<inputArray.size;i++)
	{
		if((i+1)%k==0)
			continue;
		returnArray.arr[i-(i+1)/k]=inputArray.arr[i];
	}

	return returnArray;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> differentSymbolsNaive  (0) 2020.04.05
<Codesignal> firstDigit  (0) 2020.04.05
<Codesignal> stringsRearrangement  (0) 2020.04.05
<Codesignal> absoluteValuesSumMinimization  (0) 2020.04.05
<Codesignal> depositProfit  (0) 2020.04.05

+ Recent posts