Challenge 1
Page 1 of 1
Challenge 1
[b]Challenge Question:[/b]
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
•[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.
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
•[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.
Re: Challenge 1
int adjacentElementsProduct(int[] inputArray) {
int maxproduct=-1000000;
int product;
for (int i=0; i<inputArray.length-1; i++)
{
product=inputArray[i]*inputArray[i+1];
if (product>maxproduct)
maxproduct=product;
}
return maxproduct;
}
int maxproduct=-1000000;
int product;
for (int i=0; i<inputArray.length-1; i++)
{
product=inputArray[i]*inputArray[i+1];
if (product>maxproduct)
maxproduct=product;
}
return maxproduct;
}
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|