📌  相关文章
📜  数组中最小和第二小的最大和

📅  最后修改于: 2022-05-13 01:57:09.160000             🧑  作者: Mango

数组中最小和第二小的最大和

给定一个数组,找出从所有可能的子数组中选出的最小元素和次小元素的最大和。更正式地说,如果我们写出所有 (nC2) 个大小 >=2 的数组的子数组,并找到最小和第二小的和,那么我们的答案将是它们之间的最大和。
例子:

Input : arr[] = [4, 3, 1, 5, 6]
Output : 11
Subarrays with smallest and second smallest are,
[4, 3]        smallest = 3    second smallest = 4
[4, 3, 1]    smallest = 1    second smallest = 3
[4, 3, 1, 5]    smallest = 1    second smallest = 3
[4, 3, 1, 5, 6]    smallest = 1    second smallest = 3
[3, 1]         smallest = 1    second smallest = 3
[3, 1, 5]     smallest = 1    second smallest = 3
[3, 1, 5, 6]    smallest = 1    second smallest = 3
[1, 5]        smallest = 1    second smallest = 5
[1, 5, 6]    smallest = 1    second smallest = 5
[5, 6]         smallest = 5    second smallest = 6
Maximum sum among all above choices is, 5 + 6 = 11

Input : arr[] =  {5, 4, 3, 1, 6}
Output : 9

一个简单的解决方案是生成所有子数组,找到每个子数组的最小和第二小的总和。最后返回所有总和的最大值。
一个有效的解决方案是基于观察到这个问题减少到找到数组中两个连续元素的最大和。

如果 (x,y) 是对,使得 (x+y) 是答案,那么 x 和 y 必须是数组中的连续元素。

证明:

对于具有 2 个元素的子数组,第一个和第二个最小元素是这 2 个元素。

现在 x 和 y 出现在某个子数组中,因此它们是端点。

现在,x, y 必须是该子数组中最小的 2 个元素。如果x和y之间还有其他元素Z 1 , Z 2 , ……., Z K ,它们都大于等于x和y,

情况1 :

如果在 x 和 y 之间有一个元素 z,那么包含元素 max(x,y) 和 z 的较小子数组应该是答案,因为 max(x,y) + z >= x + y

案例2:

如果 x 和 y 之间有多个元素,则 x 和 y 内的子数组将具有所有连续元素 (Z i + Z i+1 ) >= (x+y),因此 (x,y) 对可以不是答案。

因此,由于矛盾,x 和 y 必须是数组中的连续元素。

CPP
// C++ program to get max sum with smallest
// and second smallest element from any subarray
#include 
using namespace std;
 
/*  Method returns maximum obtainable sum value
    of smallest and the second smallest value
    taken over all possible subarrays */
int pairWithMaxSum(int arr[], int N)
{
   if (N < 2)
     return -1;
 
   // Find two consecutive elements with maximum
   // sum.
   int res = arr[0] + arr[1];
   for (int i=1; i


JAVA
// Java program to get max sum with smallest
// and second smallest element from any subarray
import java.lang.*;
class num{
 
// Method returns maximum obtainable sum value
// of smallest and the second smallest value
// taken over all possible subarrays */
static int pairWithMaxSum(int[] arr, int N)
{
if (N < 2)
    return -1;
 
// Find two consecutive elements with maximum
// sum.
int res = arr[0] + arr[1];
for (int i=1; i


Python3
# Python 3 program to get max
# sum with smallest and second
# smallest element from any
# subarray
 
# Method returns maximum obtainable
# sum value of smallest and the
# second smallest value taken
# over all possible subarrays
def pairWithMaxSum(arr, N):
 
    if (N < 2):
        return -1
     
    # Find two consecutive elements with
    # maximum sum.
    res = arr[0] + arr[1]
     
    for i in range(1, N-1):
        res = max(res, arr[i] + arr[i + 1])
     
    return res
     
# Driver code
arr = [4, 3, 1, 5, 6]
N = len(arr)
 
print(pairWithMaxSum(arr, N))
 
# This code is contributed by Smitha Dinesh Semwal


C#
// C# program to get max sum with smallest
// and second smallest element from any subarray
using System;
 
class GFG {
 
// Method returns maximum obtainable sum value
// of smallest and the second smallest value
// taken over all possible subarrays
static int pairWithMaxSum(int []arr, int N)
{
     
if (N < 2)
    return -1;
 
// Find two consecutive elements
// with maximum sum.
int res = arr[0] + arr[1];
for (int i = 1; i < N - 1; i++)
    res = Math.Max(res, arr[i] + arr[i + 1]);
 
return res;
}
 
// Driver code
public static void Main()
{
    int []arr = {4, 3, 1, 5, 6};
    int N = arr.Length;
    Console.Write(pairWithMaxSum(arr, N));
}
}
 
// This code is contributed by Nitin Mittal.


PHP


Javascript
// javascript program to get max sum with smallest
// and second smallest element from any subarray
   
// Method returns maximum obtainable sum value
// of smallest and the second smallest value
// taken over all possible subarrays
 
function pairWithMaxSum(arr,  N)
{
       
if (N < 2)
    return -1;
   
// Find two consecutive elements
// with maximum sum.
 
var res = arr[0] + arr[1];
for (var i = 1; i < N - 1; i++)
    res = Math.max(res, arr[i] + arr[i + 1]);
   
return res;
}
   
// Driver code
 
    var arr = [4, 3, 1, 5, 6]
    var N = arr.length;
    document.write(pairWithMaxSum(arr, N));
 
// This code is contributed by bunnyram19.


输出:

11