#include <iostream>
/** For simplicity, let's denote the length of the list by a constant value of 5.*/
const unsigned int n = 5;
/** The function gets an array of five integers as its argument: 1, 2, 3, 4, 5.
* Starting from the first list item, we multiply the current item by the next one and compare the obtained value with the third one.
* As soon as the result of multiplication of the first two items is more than the third item,
* we return the number of the first item used in multiplication.
* If there are no sequences of two items in the list whose multiplication value is greater than the third item, we return -1.
* The last two list items do not participate in the check because the third item is not in the list and there is nothing
* to compare the multiplication value with.
* In our case we will get the following verifications:
* list item 0 : 1*2 > 3 is a false;
* list item 1 : 2*3 > 4 - true -> return 1.
* On the screen you will see 1.
*/
int questionAnswer(int A[n])
{
int i = 0;
while (i < n - 2)
{
if (A[i] * A[i + 1] > A[i + 2])
{
return i;
}
i++;
}
return -1;
}
int main()
{
int A[n] = { 1,2,3,4,5 };
std::cout << questionAnswer(A);
}
Comments
Leave a comment