C ++中数组中三元组(大小为3的子序列)的最大乘积
在本教程中,我们将讨论一个程序,以查找数组中三元组(大小为3的子序列)的最大乘积。
为此,我们将提供一个整数数组。我们的任务是找到该数组中具有最大乘积的元素的三元组
示例
#include <bits/stdc++.h>
using namespace std;
//finding the maximum product
int maxProduct(int arr[], int n){
if (n < 3)
return -1;
int max_product = INT_MIN;
for (int i = 0; i < n - 2; i++)
for (int j = i + 1; j < n - 1; j++)
for (int k = j + 1; k < n; k++)
max_product = max(max_product, arr[i] * arr[j] * arr[k]);
return max_product;
}
int main() {
int arr[] = { 10, 3, 5, 6, 20 };
int n = sizeof(arr) / sizeof(arr[0]);
int max = maxProduct(arr, n);
if (max == -1)
cout << "No Triplet Exists";
else
cout << "Maximum product is " << max;
return 0;
}输出结果
Maximum product is 1200
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短