给定N个整数的数组arr [] 。任务是在每个三元组(i,j,k)中找到(arr [i] + arr [j] * arr [k])的最大值,以使arr [i]
例子:
Input: arr[]={7, 9, 3, 8, 11, 10}
Output: 106
Explanation:
The valid triplets are:
1) (7, 9, 11), and value of (arr[i] + arr[j] * arr[k]) is 106.
2) (7, 9, 10), and value of (arr[i] + arr[j] * arr[k]) is 97.
3) (7, 8, 10), and value of (arr[i] + arr[j] * arr[k]) is 87.
4) (7, 8, 11), and value of (arr[i] + arr[j] * arr[k]) is 105.
5) (3, 8, 10), and value of (arr[i] + arr[j] * arr[k]) is 83.
6) (3, 8, 11), and value of (arr[i] + arr[j] * arr[k]) is 91.
Therefore, the maximum among the values is 106
Input: arr[]={1, 2, 3}
Output: 7
天真的方法:想法是生成所有可能的有效三元组(i,j,k),并在所有三元组中打印arr [i] + arr [j] * arr [k]的最大值。步骤如下:
- 使用三个嵌套循环遍历数组。
- 对于每个有效的三元组,检查arr [i]
。如果是这样,则该三元组是有效的。 - 如果上述条件成立,则为所有三元组找到arr [i] + arr [j] * arr [k]的值,并将其存储在名为value的变量中。
- 在所有可能的三元组中,将上述表达式的值不断更新为最大值。
- 如果找不到有效的三元组,则打印-1,否则打印最大值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function that generate all valid
// triplets and calcluate the value
// of the valid triplets
void max_valid_triplet(int A[], int n)
{
int ans = -1;
// Generate all triplets
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++)
{
// Check whether the triplet
// is valid or not
if (A[i] < A[j] && A[j] < A[k])
{
int value = A[i] + A[j] * A[k];
// Update the value
if (value > ans)
{
ans = value;
}
}
}
}
}
// Print the maximum value
cout << (ans);
}
// Driver Code
int main()
{
// Given array arr[]
int arr[] = { 7, 9, 3, 8, 11, 10 };
int n = sizeof(arr) / sizeof(arr[0]);
// Function call
max_valid_triplet(arr, n);
return 0;
}
// This code is contributed by chitranayal
Java
// Java program for the above approach
import java.util.Scanner;
class GFG {
// Function that generate all valid
// triplets and calcluate the value
// of the valid triplets
static void
max_valid_triplet(int A[], int n)
{
int ans = -1;
// Generate all triplets
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++) {
// Check whether the triplet
// is valid or not
if (A[i] < A[j] && A[j] < A[k]) {
int value = A[i] + A[j] * A[k];
// Update the value
if (value > ans) {
ans = value;
}
}
}
}
}
// Print the maximum value
System.out.println(ans);
}
// Driver Code
public static void main(String args[])
{
// Given array arr[]
int[] arr = new int[] { 7, 9, 3, 8, 11, 10 };
int n = arr.length;
// Function Call
max_valid_triplet(arr, n);
}
}
Python3
# Python3 program for the above approach
# Function that generate all valid
# triplets and calcluate the value
# of the valid triplets
def max_valid_triplet(A, n):
ans = -1;
# Generate all triplets
for i in range(0, n - 2):
for j in range(i + 1, n - 1):
for k in range(j + 1, n):
# Check whether the triplet
# is valid or not
if (A[i] < A[j] and A[j] < A[k]):
value = A[i] + A[j] * A[k];
# Update the value
if (value > ans):
ans = value;
# Print the maximum value
print(ans);
# Driver Code
if __name__ == '__main__':
# Given array arr
arr = [ 7, 9, 3, 8, 11, 10 ];
n = len(arr);
# Function call
max_valid_triplet(arr, n);
# This code is contributed by Amit Katiyar
C#
// C# program for the above approach
using System;
class GFG{
// Function that generate all valid
// triplets and calcluate the value
// of the valid triplets
static void max_valid_triplet(int[] A, int n)
{
int ans = -1;
// Generate all triplets
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++)
{
// Check whether the triplet
// is valid or not
if (A[i] < A[j] && A[j] < A[k])
{
int value = A[i] + A[j] * A[k];
// Update the value
if (value > ans)
{
ans = value;
}
}
}
}
}
// Print the maximum value
Console.WriteLine(ans);
}
// Driver Code
public static void Main(String[] args)
{
// Given array []arr
int[] arr = new int[] { 7, 9, 3, 8, 11, 10 };
int n = arr.Length;
// Function Call
max_valid_triplet(arr, n);
}
}
// This code is contributed by gauravrajput1
Javascript
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function that finds the maximum
// valid triplets
static int max_valid_triplet(int A[], int n)
{
int ans = -1;
// Declare the left[] and
// right[] array
int left[] = new int[n];
int right[] = new int[n];
// Consider last element as maximum
int max = A[n - 1];
// Iterate array from the last
for (int i = n - 2; i >= 0; i--) {
// If present is less the maximum
// update the right[i] with
// previous maximum
if (max > A[i])
right[i] = max;
// Else store -1
else
right[i] = -1;
// Find the maximum for
// the next iteration
if (max < A[i])
max = A[i];
}
TreeSet set = new TreeSet();
for (int i = 1; i < n; i++) {
// Insert previous element
// to the set
set.add(A[i - 1]);
Integer result = set.lower(A[i]);
// Search for maximum element
// which is < present element
// If result is null there is
// no such element exists
// so store -1 at left[i]
if (result == null)
left[i] = -1;
// Else store the result
else
left[i] = result;
}
// Traverse the original array
for (int i = 1; i < n - 1; i++) {
// Condition for valid triplet
if (left[i] != -1
&& right[i] != -1)
// Find the value and update
// the maximum value
ans = Math.max(ans,
left[i] + A[i] * right[i]);
}
// Return the ans
return ans;
}
// Driver Code
public static void main(String args[])
{
// Given array arr[]
int[] A = new int[] { 7, 9, 3, 8, 11, 10 };
int n = A.length;
// Function Call
System.out.println(max_valid_triplet(A, n));
}
}
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function that finds the maximum
// valid triplets
static int max_valid_triplet(int []A, int n)
{
int ans = -1;
// Declare the []left and
// []right array
int []left = new int[n];
int []right = new int[n];
// Consider last element as maximum
int max = A[n - 1];
// Iterate array from the last
for(int i = n - 2; i >= 0; i--)
{
// If present is less the maximum
// update the right[i] with
// previous maximum
if (max > A[i])
right[i] = max;
// Else store -1
else
right[i] = -1;
// Find the maximum for
// the next iteration
if (max < A[i])
max = A[i];
}
SortedSet set = new SortedSet();
for(int i = 1; i < n; i++)
{
// Insert previous element
// to the set
set.Add(A[i - 1]);
int result = set.Min;
// Search for maximum element
// which is < present element
// If result is null there is
// no such element exists
// so store -1 at left[i]
if (result == 0)
left[i] = -1;
// Else store the result
else
left[i] = result;
}
// Traverse the original array
for(int i = 1; i < n - 1; i++)
{
// Condition for valid triplet
if (left[i] != -1 &&
right[i] != -1)
// Find the value and update
// the maximum value
ans = Math.Max(ans,
left[i] +
A[i] *
right[i]);
}
// Return the ans
return ans;
}
// Driver Code
public static void Main(String []args)
{
// Given array []arr
int[] A = new int[]{ 7, 9, 3, 8, 11, 10 };
int n = A.Length;
// Function call
Console.WriteLine(max_valid_triplet(A, n));
}
}
// This code is contributed by Amit Katiyar
106
时间复杂度: O(N 3 )
辅助空间: O(1)
高效的方法:可以通过使用Java的TreeSet来优化上述方法。步骤如下:
- 创建两个数组。一个数组(左)用于存储左侧的最大元素,该元素严格小于原始数组中的当前元素;另一个数组(右)用于存储左侧的最大元素,如下所示。数组arr [] = {7,9,3,8,11,10}的图片:
- 对于左侧数组的构造,我们使用Java的TreeSet,将元素插入TreeSet中,使用TreeSet中的lower()方法,该方法将返回此集合中最大的元素,该元素严格小于给定的元素。如果此TreeSet集合中不存在这样的元素,则此方法返回NULL。
- 左侧数组中的元素将为有效三连串的arr [i] ,右侧数组中的元素将为有效三元组的arr [k] 。
- 现在,遍历原始数组从1到N – 1 ,为有效的三元组选择arr [j] 。
- 如果left [i]!=-1 && right [i]!=-1,则有形成三重态的机会。
- 为所有这样的有效三元组找到值arr [i] + arr [j] * arr [k] ,并根据最大值更新ans。
- 打印最大值(如果存在),否则打印“ -1” 。
下面是上述方法的实现:
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function that finds the maximum
// valid triplets
static int max_valid_triplet(int A[], int n)
{
int ans = -1;
// Declare the left[] and
// right[] array
int left[] = new int[n];
int right[] = new int[n];
// Consider last element as maximum
int max = A[n - 1];
// Iterate array from the last
for (int i = n - 2; i >= 0; i--) {
// If present is less the maximum
// update the right[i] with
// previous maximum
if (max > A[i])
right[i] = max;
// Else store -1
else
right[i] = -1;
// Find the maximum for
// the next iteration
if (max < A[i])
max = A[i];
}
TreeSet set = new TreeSet();
for (int i = 1; i < n; i++) {
// Insert previous element
// to the set
set.add(A[i - 1]);
Integer result = set.lower(A[i]);
// Search for maximum element
// which is < present element
// If result is null there is
// no such element exists
// so store -1 at left[i]
if (result == null)
left[i] = -1;
// Else store the result
else
left[i] = result;
}
// Traverse the original array
for (int i = 1; i < n - 1; i++) {
// Condition for valid triplet
if (left[i] != -1
&& right[i] != -1)
// Find the value and update
// the maximum value
ans = Math.max(ans,
left[i] + A[i] * right[i]);
}
// Return the ans
return ans;
}
// Driver Code
public static void main(String args[])
{
// Given array arr[]
int[] A = new int[] { 7, 9, 3, 8, 11, 10 };
int n = A.length;
// Function Call
System.out.println(max_valid_triplet(A, n));
}
}
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function that finds the maximum
// valid triplets
static int max_valid_triplet(int []A, int n)
{
int ans = -1;
// Declare the []left and
// []right array
int []left = new int[n];
int []right = new int[n];
// Consider last element as maximum
int max = A[n - 1];
// Iterate array from the last
for(int i = n - 2; i >= 0; i--)
{
// If present is less the maximum
// update the right[i] with
// previous maximum
if (max > A[i])
right[i] = max;
// Else store -1
else
right[i] = -1;
// Find the maximum for
// the next iteration
if (max < A[i])
max = A[i];
}
SortedSet set = new SortedSet();
for(int i = 1; i < n; i++)
{
// Insert previous element
// to the set
set.Add(A[i - 1]);
int result = set.Min;
// Search for maximum element
// which is < present element
// If result is null there is
// no such element exists
// so store -1 at left[i]
if (result == 0)
left[i] = -1;
// Else store the result
else
left[i] = result;
}
// Traverse the original array
for(int i = 1; i < n - 1; i++)
{
// Condition for valid triplet
if (left[i] != -1 &&
right[i] != -1)
// Find the value and update
// the maximum value
ans = Math.Max(ans,
left[i] +
A[i] *
right[i]);
}
// Return the ans
return ans;
}
// Driver Code
public static void Main(String []args)
{
// Given array []arr
int[] A = new int[]{ 7, 9, 3, 8, 11, 10 };
int n = A.Length;
// Function call
Console.WriteLine(max_valid_triplet(A, n));
}
}
// This code is contributed by Amit Katiyar
106
时间复杂度: O(N)
辅助空间: O(N)