Easy

Given an array of integers, find the maximal absolute difference between any two of its adjacent elements.

Example

For inputArray = [2, 4, 1, 0], the output should be
arrayMaximalAdjacentDifference(inputArray) = 3.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer inputArray

    Guaranteed constraints:
    3 ≤ inputArray.length ≤ 10,
    -15 ≤ inputArray[i] ≤ 15.

  • [output] integer

    • The maximal absolute difference.

[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;
// }
//
//
int arrayMaximalAdjacentDifference(arr_integer inputArray)
{
	int max=0;

	for(int i=1;i<inputArray.size;i++)
		max=abs(inputArray.arr[i]-inputArray.arr[i-1])>max?abs(inputArray.arr[i]-inputArray.arr[i-1]):max;

	return max;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> avoidObstacles  (0) 2020.04.05
<Codesignal> isIPv4Address  (0) 2020.04.05
<Codesignal> areEquallyStrong  (0) 2020.04.05
<Codesignal> palindromeRearranging  (0) 2020.04.05
<Codesignal> arrayChange  (0) 2020.04.05

+ Recent posts