给定一个由N 个整数组成的数组arr[] 。任务是在每个三元组(i, j, k) 中找到(arr[i] + arr[j] * arr[k])的最大值,使得arr[i] < arr[j] < arr[k] ]和i < j < k 。如果不存在任何这样的三元组,则打印“-1” 。
例子:
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[j] < arr[k] 。如果是,那么三元组是有效的。
- 如果上述条件为真,则为所有这些三元组找出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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live