Easy

We define the middle of the array arr as follows:

  • if arr contains an odd number of elements, its middle is the element whose index number is the same when counting from the beginning of the array and from its end;
  • if arr contains an even number of elements, its middle is the sum of the two elements whose index numbers when counting from the beginning and from the end of the array differ by one.

An array is called smooth if its first and its last elements are equal to one another and to the middle. Given an array arr, determine if it is smooth or not.

Example

  • For arr = [7, 2, 2, 5, 10, 7], the output should be
    isSmooth(arr) = true.

    The first and the last elements of arr are equal to 7, and its middle also equals 2 + 5 = 7. Thus, the array is smooth and the output is true.

  • For arr = [-5, -5, 10], the output should be
    isSmooth(arr) = false.

    The first and middle elements are equal to -5, but the last element equals 10. Thus, arr is not smooth and the output is false.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer arr

    The given array.

    Guaranteed constraints:
    2 ≤ arr.length ≤ 10^5,
    -10^9 ≤ arr[i] ≤ 10^9.

  • [output] boolean

    • true if arr is smooth, false otherwise.

[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;
// }
//
//
bool isSmooth(arr_integer arr)
{
	return arr.size%2==1?(arr.arr[arr.size/2]==arr.arr[0]&&arr.arr[0]==arr.arr[arr.size-1]):(arr.arr[arr.size/2]+arr.arr[arr.size/2-1]==arr.arr[0]&&arr.arr[0]==arr.arr[arr.size-1]);
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Make Array Consecutive 2  (0) 2020.04.15
<Codesignal> Replace Middle  (0) 2020.04.14
<Codesignal> Remove Array Part  (0) 2020.04.14
<Codesignal> Concatenate Arrays  (0) 2020.04.14
<Codesignal> First Reverse Try  (0) 2020.04.14

+ Recent posts