Easy

Given an array of integers, find the pair of adjacent elements that has the largest product and return that product.

Example

For inputArray = [3, 6, -2, -5, 7, 3], the output should be
adjacentElementsProduct(inputArray) = 21.

7 and 3 produce the largest product.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer inputArray

    An array of integers containing at least two elements.

    Guaranteed constraints:
    2 ≤ inputArray.length ≤ 10,
    -1000 ≤ inputArray[i] ≤ 1000.

  • [output] integer

    • The largest product of adjacent elements.

[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 adjacentElementsProduct(arr_integer inputArray)
{
	int max=-1000;

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

	return max;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> almostIncreasingSequence  (0) 2020.04.05
<Codesignal> shapeArea  (0) 2020.04.05
<Codesignal> checkPalindrome  (0) 2020.04.05
<Codesignal> centuryFromYear  (0) 2020.04.05
<Codesignal> add  (0) 2020.04.05

+ Recent posts