The challenge on CodeSignal is the following:
Given an array of integers, find the pair of adjacent elements that has the largest product and return that product.
My plan is to create a secondArray offset by 1 to the right and then multiple inputArray by secondArray. This will give the product of a pair of adjacent elements. After I will return the max of the productArray as the solution. I can't seem to get past the first hurdle, that is to shift the elements of the array by 1 to give the secondArray.
`
// 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 solution(arr_integer inputArray) {
int productArray;
productArray = inputArray[0] * inputArray[0 ];
}
`
CodePudding user response:
C does not have any built-in notation that would multiply two arrays nor any built in notation that says to give a view of an array that is offset by one element from its normal base (although pointers can be used to that effect).
To compute the desired products, you should write a loop that iterates over the positions in the array.
It is generally good advice to study C through a primer or textbook rather than attempting to learn it by working on online contests or challenges.
CodePudding user response:
You don't need to waste memory by "cloning" the original array; simply iterate over the number of adjacent pairs, calculate the product, and see if it is the largest so far.
There are easily at least a dozen different specific ways to do it that come to mind immediately, and you could probably gin up some more if you gave it some thought. This way uses pointers to the adjacent elements in each pair.
#include <stdio.h>
#define SIZE 10
int solution(int *arr, int arrayLen);
int main(int argc, char* argv[]) {
int arr[SIZE] = {11, 22, 43, 44, 59, 36, 27, 58, 19, 20};
printf("%d is the largest", solution(arr, SIZE));
return 0;
}
int solution(int *arr, int arrayLen) {
int max = 0;
if (arr) {
int *p1 = arr;
int *p2 = p1 1;
// for a given array of size N, there are N - 1 adjacent pairs
// so we iterate over the NUMBER OF PAIRS
for (int i = 0; i < arrayLen - 1; i ) {
int value = *p1 * *p2 ;
if (value > max) {
max = value;
}
}
}
return max;
}
CodePudding user response:
Unfortunately, I don't understand your question 100%. If it is about the question in the title (How do I clone an array and move the index by 1 in C?), you can clone the string with memcpy and then move all elements by 1.
// Clone the array
int given_array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int cloned_array[10];
memcpy(cloned_array, given_array, sizeof(original_array));
// Move the index
for (int i = 0; i < 10; i ) {
cloned_array[i] = 1;
}
If it is about the question in your question, you can proceed as follows: Your first approach was good:, but after that you still have to find the pair with the biggest product.
//Here you should add the size of the array
int solution(arr_integer inputArray, int size) {
int productArray;
productArray = inputArray[0] * inputArray[1];
But after that you still have to find the pair with the biggest product. You can do this by looping through the array:
for (int i = 1; i < size - 1; i ) {
int tmp = arr[i] * arr[i 1];
if (tmp > productArray) {
productArray = tmp;
}
}
Of cause, you have to return this solution in the end :)
return productArray
I hope that helps