📜  找到给定数组中最大的连续对和

📅  最后修改于: 2021-09-06 06:25:14             🧑  作者: Mango

给定一个大小为N的整数数组arr[] ,任务是找到连续对{a, b}使得该对中两个元素的总和最大。如果有不止一个这样的对的最大总和,则打印任何这样的对。在多对的总和最大的情况下,打印其中任何一对。
例子:

方法:
请按照以下步骤解决问题:

  1. 一一生成所有连续对并计算总和。
  2. 将每对的总和与最大总和进行比较,并相应地更新与最大总和相对应的对。
  3. 返回代表最大总和的对。

下面是上述方法的实现:

C++
// C++ program to find the 
// a contiguous pair from the
// which has the largest sum
#include 
using namespace std;
  
// Function to find and return
// the largest sum contiguous pair
vector largestSumpair(int arr[], int n)
{
      
    // Stores the contiguous pair 
    vector pair;
      
    // Intialize maximum sum
    int max_sum = INT_MIN, i;
      
    for(i = 1; i < n; i++) 
    {
          
        // Compare sum of pair with max_sum
        if (max_sum < (arr[i] + arr[i - 1]))
        {
            max_sum = arr[i] + arr[i - 1];
            if (pair.empty()) 
            { 
                  
                // Insert the pair
                pair.push_back(arr[i - 1]);
                pair.push_back(arr[i]);
            }
            else
            {
                pair[0] = arr[i - 1];
                pair[1] = arr[i];
            }
        }
        return pair;
    }
}
      
// Driver Code 
int main() 
{ 
    int arr[] = {11, -5, 9, -3, 2};
    int N = sizeof(arr) / sizeof(arr[0]);
      
    vector pair = largestSumpair(arr, N);
    for(auto it = pair.begin(); it != pair.end(); ++it)
    {
        cout << *it << ' '; 
    }
      
    return 0; 
} 
  
// This code is contributed by shubhamsingh10


Java
// Java program to find the 
// a contiguous pair from the 
// which has the largest sum 
import java.util.*;
  
class GFG{
      
// Function to find and return 
// the largest sum contiguous pair 
public static Vector largestSumpair(int[] arr,
                                             int n) 
{ 
      
    // Stores the contiguous pair 
    Vector pair = new Vector(); 
      
    // Intialize maximum sum 
    int max_sum = Integer.MIN_VALUE, i; 
      
    for(i = 1; i < n; i++) 
    { 
          
        // Compare sum of pair with max_sum 
        if (max_sum < (arr[i] + arr[i - 1])) 
        { 
            max_sum = arr[i] + arr[i - 1]; 
              
            if (pair.isEmpty()) 
            { 
                  
                // Insert the pair 
                pair.add(arr[i - 1]); 
                pair.add(arr[i]); 
            } 
            else
            { 
                pair.set(0, arr[i - 1]);
                pair.set(1, arr[i]);
            } 
        } 
    } 
    return pair;
} 
  
// Driver Code    
public static void main(String[] args) 
{
    int arr[] = { 11, -5, 9, -3, 2 }; 
    int N = arr.length; 
      
    Vector pair = new Vector(); 
    pair = largestSumpair(arr, N);
    for (Integer it : pair)
    {         
        System.out.print(it + " ");
    }
}
}
  
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program to find the 
# a contiguous pair from the
# which has the largest sum
  
# importing sys
import sys
  
# Function to find and return
# the largest sum contiguous pair
def largestSumpair(arr, n):
# Stores the contiguous pair 
    pair = []
  
# Intialize maximum sum
    max_sum = -sys.maxsize-1
  
    for i in range(1, n):
      
        # Compare sum of pair with max_sum
        if max_sum < ( arr[i] + arr[i-1] ):
            max_sum = arr[i] + arr[i-1]
          
            if pair == []:
                # Insert the pair
                pair.append(arr[i-1])
                pair.append(arr[i])
            else:
                pair[0] = arr[i-1]
                pair[1] = arr[i]
  
    return pair
      
      
# Driver Code 
arr = [11, -5, 9, -3, 2] 
N = len(arr) 
pair = largestSumpair(arr, N)
print(pair[0], end =" ")
print(pair[1], end =" ")


C#
// C# program to find the 
// a contiguous pair from the 
// which has the largest sum 
using System;
using System.Collections; 
using System.Collections.Generic;
  
class GFG{
  
// Function to find and return 
// the largest sum contiguous pair 
public static ArrayList largestSumpair(int[] arr,
                                       int n) 
{ 
      
    // Stores the contiguous pair 
    ArrayList pair = new ArrayList(); 
      
    // Intialize maximum sum 
    int max_sum = int.MinValue, i; 
      
    for(i = 1; i < n; i++) 
    { 
          
        // Compare sum of pair with max_sum 
        if (max_sum < (arr[i] + arr[i - 1])) 
        { 
            max_sum = arr[i] + arr[i - 1]; 
              
            if (pair.Count == 0) 
            { 
                  
                // Insert the pair 
                pair.Add(arr[i - 1]); 
                pair.Add(arr[i]); 
            } 
            else
            { 
                pair[0] = arr[i - 1];
                pair[1] = arr[i];
            } 
        } 
    } 
    return pair;
} 
  
// Driver code
public static void Main(string[] args)
{
    int []arr = { 11, -5, 9, -3, 2 }; 
    int N = arr.Length; 
      
    ArrayList pair = new ArrayList(); 
    pair = largestSumpair(arr, N);
      
    foreach(int it in pair)
    {         
        Console.Write(it + " ");
    }
}
}
  
// This code is contributed by rutvik_56


输出:
11 -5

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live