Easy

Reversing an array can be a tough task, especially for a novice programmer. Mary just started coding, so she would like to start with something basic at first. Instead of reversing the array entirely, she wants to swap just its first and last elements.

Given an array arr, swap its first and last elements and return the resulting array.

Example

For arr = [1, 2, 3, 4, 5], the output should be
firstReverseTry(arr) = [5, 2, 3, 4, 1].

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer arr

    Guaranteed constraints:
    0 ≤ arr.length ≤ 50,
    -10^4 ≤ arr[i] ≤ 10^4.

  • [output] array.integer

    • Array arr with its first and its last elements swapped.

[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 firstReverseTry(arr_integer arr)
{
	if(arr.size==0)
		return arr;

	int temp=arr.arr[0];
	arr.arr[0]=arr.arr[arr.size-1];
	arr.arr[arr.size-1]=temp;

	return arr;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Remove Array Part  (0) 2020.04.14
<Codesignal> Concatenate Arrays  (0) 2020.04.14
<Codesignal> Create Array  (0) 2020.04.14
<Codesignal> Count Black Cells  (0) 2020.04.14
<Codesignal> Candles  (0) 2020.04.13

+ Recent posts