📜  与数组中小于K的最大和配对

📅  最后修改于: 2021-04-22 03:18:08             🧑  作者: Mango

给定大小为N的数组arr和整数K。任务是找到一对整数,使它们的总和最大但小于K
例子:

方法 :
一种有效的方法是对给定的数组进行排序并找到大于或等于K的元素。如果在索引p处找到,我们必须仅在arr [0,…,p-1]之间找到对。运行嵌套循环。一个照顾一对中的第一个元素,另一个照顾一对中的第二个元素。保持变量maxsum以及其他两个变量ab来跟踪可能的解决方案。将maxsum初始化为0 。找出A [i] + A [j] (假设i在外循环中运行,j在内循环中运行)。如果它大于maxsum,则用该和更新maxsum并将ab更改为该数组的第i和j个元素。
下面是上述方法的实现:

C++
// CPP program to find pair with largest
// sum which is less than K in the array
#include 
using namespace std;
 
// Function to find pair with largest
// sum which is less than K in the array
void Max_Sum(int arr[], int n, int k)
{
    // To store the break point
    int  p = n;
     
    // Sort the given array
    sort(arr, arr + n);
     
    // Find the break point
    for (int i = 0; i < n; i++)
    {
         // No need to look beyond i'th index
        if (arr[i] >= k) {
            p = i;
            break;
        }
    }
     
     
    int maxsum = 0, a, b;
     
    // Find the required pair
    for (int i = 0; i < p; i++)
    {
        for (int j = i + 1; j < p; j++)
        {
            if (arr[i] + arr[j] < k and arr[i] + arr[j] >
                                                     maxsum)
            {
                maxsum = arr[i] + arr[j];
                 
                a = arr[i];
                b = arr[j];
            }
        }
    }
     
    // Print the required answer
    cout << a << " " << b;
}
 
// Driver code
int main()
{
    int arr[] = {5, 20, 110, 100, 10}, k = 85;
     
    int n = sizeof(arr) / sizeof(arr[0]);
     
    // Function call
    Max_Sum(arr, n, k);
     
    return 0;
}


Java
// Java program to find pair with largest
// sum which is less than K in the array
import java.util.Arrays;
 
class GFG
{
 
// Function to find pair with largest
// sum which is less than K in the array
static void Max_Sum(int arr[], int n, int k)
{
    // To store the break point
    int p = n;
     
    // Sort the given array
    Arrays.sort(arr);
     
    // Find the break point
    for (int i = 0; i < n; i++)
    {
        // No need to look beyond i'th index
        if (arr[i] >= k)
        {
            p = i;
            break;
        }
    }
     
    int maxsum = 0, a = 0, b = 0;
     
    // Find the required pair
    for (int i = 0; i < p; i++)
    {
        for (int j = i + 1; j < p; j++)
        {
            if (arr[i] + arr[j] < k &&
                arr[i] + arr[j] > maxsum)
            {
                maxsum = arr[i] + arr[j];
                 
                a = arr[i];
                b = arr[j];
            }
        }
    }
     
    // Print the required answer
    System.out.print( a + " " + b);
}
 
// Driver code
public static void main (String[] args)
{
    int []arr = {5, 20, 110, 100, 10};
    int k = 85;
 
    int n = arr.length;
     
    // Function call
    Max_Sum(arr, n, k);
}
}
 
// This code is contributed by anuj_67..


Python3
# Python3 program to find pair with largest
# sum which is less than K in the array
 
# Function to find pair with largest
# sum which is less than K in the array
def Max_Sum(arr, n, k):
 
    # To store the break point
    p = n
     
    # Sort the given array
    arr.sort()
     
    # Find the break point
    for i in range(0, n):
         
        # No need to look beyond i'th index
        if (arr[i] >= k):
            p = i
            break
     
    maxsum = 0
    a = 0
    b = 0
     
    # Find the required pair
    for i in range(0, p):    
        for j in range(i + 1, p):
            if(arr[i] + arr[j] < k and
               arr[i] + arr[j] > maxsum):
                maxsum = arr[i] + arr[j]
                a = arr[i]
                b = arr[j]
                 
    # Print the required answer
    print(a, b)
 
# Driver code
arr = [5, 20, 110, 100, 10]
k = 85
 
n = len(arr)
 
# Function call
Max_Sum(arr, n, k)
 
# This code is contributed by Sanjit_Prasad


C#
// C# program to find pair with largest
// sum which is less than K in the array
using System;
 
class GFG
{
 
// Function to find pair with largest
// sum which is less than K in the array
static void Max_Sum(int []arr, int n, int k)
{
    // To store the break point
    int p = n;
     
    // Sort the given array
    Array.Sort(arr);
     
    // Find the break point
    for (int i = 0; i < n; i++)
    {
        // No need to look beyond i'th index
        if (arr[i] >= k)
        {
            p = i;
            break;
        }
    }
     
    int maxsum = 0, a = 0, b = 0;
     
    // Find the required pair
    for (int i = 0; i < p; i++)
    {
        for (int j = i + 1; j < p; j++)
        {
            if (arr[i] + arr[j] < k &&
                arr[i] + arr[j] > maxsum)
            {
                maxsum = arr[i] + arr[j];
                 
                a = arr[i];
                b = arr[j];
            }
        }
    }
     
    // Print the required answer
    Console.WriteLine( a + " " + b);
}
 
// Driver code
public static void Main ()
{
    int []arr = {5, 20, 110, 100, 10};
    int k = 85;
 
    int n = arr.Length;
     
    // Function call
    Max_Sum(arr, n, k);
}
}
 
// This code is contributed by anuj_67..


C++
// CPP program for the above approach
#include 
using namespace std;
 
// Function to find max sum less than k
int maxSum(vector arr, int k)
{
     
    // Sort the array
    sort(arr.begin(), arr.end());
    int n = arr.size(), l = 0, r = n - 1, ans = 0;
 
    // While l is less than r
    while (l < r) {
        if (arr[l] + arr[r] < k) {
            ans = max(ans, arr[l] + arr[r]);
            l++;
        }
        else {
            r--;
        }
    }
 
    return ans;
}
 
// Driver Code
int main()
{
    vector A = { 20, 10, 30, 100, 110 };
    int k = 85;
 
    // Function Call
    cout << maxSum(A, k) << endl;
}


输出
10 20

时间复杂度: O(N ^ 2)

高效方法:两指针方法
排序后,我们可以在数组的左右两端放置两个指针。然后可以通过以下步骤找到所需的一对:

  1. 在两个指针处找到当前值的总和。
  2. 如果总和小于k:
    1. 使用先前答案的最大值和当前总和来更新答案。
    2. 增加左指针。
  3. 其他减少右指针。

C++

// CPP program for the above approach
#include 
using namespace std;
 
// Function to find max sum less than k
int maxSum(vector arr, int k)
{
     
    // Sort the array
    sort(arr.begin(), arr.end());
    int n = arr.size(), l = 0, r = n - 1, ans = 0;
 
    // While l is less than r
    while (l < r) {
        if (arr[l] + arr[r] < k) {
            ans = max(ans, arr[l] + arr[r]);
            l++;
        }
        else {
            r--;
        }
    }
 
    return ans;
}
 
// Driver Code
int main()
{
    vector A = { 20, 10, 30, 100, 110 };
    int k = 85;
 
    // Function Call
    cout << maxSum(A, k) << endl;
}
输出
50

时间复杂度:O(NlogN)

空间复杂度:O(1)