Given an integer array of size n write a function that finds the maximum product of two integers in it.
using namespace std;
void GetMaxProduct(int arr[], int n)
{
int MaxP = INT_MIN;
int ii, jj,i,j;
if (n < 2) return;
for (i = 0; i < n - 1; i++)
{
for (j = i + 1; j < n; j++)
{
if (MaxP < arr[i] * arr[j])
{
MaxP = arr[i] * arr[j];
ii = i;
jj = j;
}
}
}
cout<<"\n\tMax. Product Pair: ("<<arr[ii]<<", "<<arr[jj]<<")";
}
int main()
{
int l,Num[] = { -20, -3, 4, 5, -2 };
l = sizeof(Num) / sizeof(Num[0]);
GetMaxProduct(Num, l);
return 0;
}
Comments
Leave a comment