Easy

Remove a part of a given array between given 0-based indexes l and r (inclusive).

Example

For inputArray = [2, 3, 2, 3, 4, 5], l = 2, and r = 4, the output should be
removeArrayPart(inputArray, l, r) = [2, 3, 5].

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer inputArray

    Guaranteed constraints:
    2 ≤ inputArray.length ≤ 10^4,
    -10^5 ≤ inputArray[i] ≤ 10^5.

  • [input] integer l

    Left index of the part to be removed (0-based).

    Guaranteed constraints:
    0 ≤ l ≤ r.

  • [input] integer r

    Right index of the part to be removed (0-based).

    Guaranteed constraints:
    l ≤ r < inputArray.length.

  • [output] array.integer

[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 removeArrayPart(arr_integer inputArray,int l,int r)
{
	arr_integer returnArray=alloc_arr_integer(inputArray.size-r+l-1);

	for(int i=0;i<returnArray.size;i++)
		returnArray.arr[i]=i<l?inputArray.arr[i]:inputArray.arr[i+r-l+1];

	return returnArray;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Replace Middle  (0) 2020.04.14
<Codesignal> Is Smooth?  (0) 2020.04.14
<Codesignal> Concatenate Arrays  (0) 2020.04.14
<Codesignal> First Reverse Try  (0) 2020.04.14
<Codesignal> Create Array  (0) 2020.04.14

+ Recent posts