📜  具有特定差异的对的最大和

📅  最后修改于: 2021-09-22 09:56:06             🧑  作者: Mango

给定一个整数数组和一个数字 k。如果它们之间的差异严格小于 k,我们可以将数组的两个数字配对。任务是找到不相交对的最大可能总和。 P对的总和是所有2P对数的总和。

例子:

方法:
首先,我们按递增顺序对给定数组进行排序。一旦数组被排序,我们就遍历数组。对于每个元素,我们首先尝试将其与其前一个元素配对。为什么我们更喜欢前一个元素?让 arr[i] 可以与 arr[i-1] 和 arr[i-2] 配对(即 arr[i] – arr[i-1] < K and arr[i]-arr[i-2] < K)。由于数组已排序,因此 arr[i-1] 的值将大于 arr[i-2]。另外,我们需要对小于k的差值进行配对,这意味着如果arr[i-2]可以配对,那么arr[i-1]也可以在排序数组中配对。
现在观察上述事实,我们可以制定我们的动态规划解决方案如下,
让 dp[i] 表示我们可以使用数组的前 i 个元素实现的最大不相交对和。假设目前,我们在第 i 个位置,那么我们有两种可能性。

Pair up i with (i-1)th element, i.e. 
      dp[i] = dp[i-2] + arr[i] + arr[i-1]
  Don't pair up, i.e. 
      dp[i] = dp[i-1] 

以上迭代需要 O(N) 时间,数组排序需要 O(N log N) 时间,因此解决方案的总时间复杂度为 O(N log N)

C++
// C++ program to find maximum pair sum whose
// difference is less than K
#include 
using namespace std;
 
// method to return maximum sum we can get by
// finding less than K difference pair
int maxSumPairWithDifferenceLessThanK(int arr[], int N, int K)
{
    // Sort input array in ascending order.
    sort(arr, arr+N);
 
    // dp[i] denotes the maximum disjoint pair sum
    // we can achieve using first i elements
    int dp[N];
 
    //  if no element then dp value will be 0
    dp[0] = 0;
 
    for (int i = 1; i < N; i++)
    {
        // first give previous value to dp[i] i.e.
        // no pairing with (i-1)th element
        dp[i] = dp[i-1];
 
        // if current and previous element can form a pair
        if (arr[i] - arr[i-1] < K)
        {
            // update dp[i] by choosing maximum between
            // pairing and not pairing
            if (i >= 2)
                dp[i] = max(dp[i], dp[i-2] + arr[i] + arr[i-1]);
            else
                dp[i] = max(dp[i], arr[i] + arr[i-1]);
        }
    }
 
    //  last index will have the result
    return dp[N - 1];
}
 
//  Driver code to test above methods
int main()
{
    int arr[] = {3, 5, 10, 15, 17, 12, 9};
    int N = sizeof(arr)/sizeof(int);
 
    int K = 4;
    cout << maxSumPairWithDifferenceLessThanK(arr, N, K);
    return 0;
}


Java
// Java program to find maximum pair sum whose
// difference is less than K
 
import java.io.*;
import java.util.*;
 
class GFG {
     
    // method to return maximum sum we can get by
    // finding less than K difference pair
    static int maxSumPairWithDifferenceLessThanK(int arr[],
                                               int N, int K)
    {
         
        // Sort input array in ascending order.
        Arrays.sort(arr);
     
        // dp[i] denotes the maximum disjoint pair sum
        // we can achieve using first i elements
        int dp[] = new int[N];
     
        // if no element then dp value will be 0
        dp[0] = 0;
     
        for (int i = 1; i < N; i++)
        {
            // first give previous value to dp[i] i.e.
            // no pairing with (i-1)th element
            dp[i] = dp[i-1];
     
            // if current and previous element can form a pair
            if (arr[i] - arr[i-1] < K)
            {
                 
                // update dp[i] by choosing maximum between
                // pairing and not pairing
                if (i >= 2)
                    dp[i] = Math.max(dp[i], dp[i-2] + arr[i] +
                                                    arr[i-1]);
                else
                    dp[i] = Math.max(dp[i], arr[i] + arr[i-1]);
            }
        }
     
        // last index will have the result
        return dp[N - 1];
    }
 
    // Driver code to test above methods
    public static void main (String[] args) {
         
        int arr[] = {3, 5, 10, 15, 17, 12, 9};
        int N = arr.length;
        int K = 4;
         
        System.out.println ( maxSumPairWithDifferenceLessThanK(
                                                    arr, N, K));
         
    }
}
 
//This code is contributed by vt_m.


Python3
# Python3 program to find maximum pair
# sum whose difference is less than K
 
# method to return maximum sum we can
# get by get by finding less than K
# difference pair
def maxSumPairWithDifferenceLessThanK(arr, N, K):
 
    # Sort input array in ascending order.
    arr.sort()
 
    # dp[i] denotes the maximum disjoint
    # pair sum we can achieve using first
    # i elements
    dp = [0] * N
 
    # if no element then dp value will be 0
    dp[0] = 0
 
    for i in range(1, N):
     
        # first give previous value to
        # dp[i] i.e. no pairing with
        # (i-1)th element
        dp[i] = dp[i-1]
 
        # if current and previous element
        # can form a pair
        if (arr[i] - arr[i-1] < K):
         
            # update dp[i] by choosing
            # maximum between pairing
            # and not pairing
            if (i >= 2):
                dp[i] = max(dp[i], dp[i-2] + arr[i] + arr[i-1]);
            else:
                dp[i] = max(dp[i], arr[i] + arr[i-1]);
         
    # last index will have the result
    return dp[N - 1]
 
# Driver code to test above methods
arr = [3, 5, 10, 15, 17, 12, 9]
N = len(arr)
K = 4
print(maxSumPairWithDifferenceLessThanK(arr, N, K))
 
# This code is contributed by Smitha Dinesh Semwal


C#
// C# program to find maximum pair sum whose
// difference is less than K
using System;
 
class GFG {
     
    // method to return maximum sum we can get by
    // finding less than K difference pair
    static int maxSumPairWithDifferenceLessThanK(int []arr,
                                              int N, int K)
    {
         
        // Sort input array in ascending order.
        Array.Sort(arr);
     
        // dp[i] denotes the maximum disjoint pair sum
        // we can achieve using first i elements
        int []dp = new int[N];
     
        // if no element then dp value will be 0
        dp[0] = 0;
     
        for (int i = 1; i < N; i++)
        {
            // first give previous value to dp[i] i.e.
            // no pairing with (i-1)th element
            dp[i] = dp[i-1];
     
            // if current and previous element can form
            // a pair
            if (arr[i] - arr[i-1] < K)
            {
                 
                // update dp[i] by choosing maximum
                // between pairing and not pairing
                if (i >= 2)
                    dp[i] = Math.Max(dp[i], dp[i-2]
                                + arr[i] + arr[i-1]);
                else
                    dp[i] = Math.Max(dp[i], arr[i]
                                        + arr[i-1]);
            }
        }
     
        // last index will have the result
        return dp[N - 1];
    }
 
    // Driver code to test above methods
    public static void Main () {
         
        int []arr = {3, 5, 10, 15, 17, 12, 9};
        int N = arr.Length;
        int K = 4;
         
        Console.WriteLine(
          maxSumPairWithDifferenceLessThanK(arr, N, K));
         
    }
}
 
// This code is contributed by anuj_67.


PHP
= 2)
                $dp[$i] = max($dp[$i], $dp[$i-2] + $arr[$i] + $arr[$i-1]);
            else
                $dp[$i] = max($dp[$i], $arr[$i] + $arr[$i-1]);
        }
    }
 
    // last index will have the result
    return $dp[$N - 1];
}
 
    // Driver code
 
    $arr = array(3, 5, 10, 15, 17, 12, 9);
    $N = sizeof($arr) ;
 
    $K = 4;
    echo maxSumPairWithDifferenceLessThanK($arr, $N, $K);
 
 
    // This code is contributed by Ryuga
?>


Javascript


C++
// C++ program to find maximum pair sum whose
// difference is less than K
#include 
using namespace std;
 
// Method to return maximum sum we can get by
// finding less than K difference pairs
int maxSumPair(int arr[], int N, int k)
{
    int maxSum = 0;
 
    // Sort elements to ensure every i and i-1 is closest
    // possible pair
    sort(arr, arr + N);
 
    // To get maximum possible sum,
    // iterate from largest to
    // smallest, giving larger
    // numbers priority over smaller
    // numbers.
    for (int i = N - 1; i > 0; --i)
    {
        // Case I: Diff of arr[i] and arr[i-1]
        //  is less then K,add to maxSum      
        // Case II: Diff between arr[i] and arr[i-1] is not
        // less then K, move to next i since with
        // sorting we know, arr[i]-arr[i-1] <
        // rr[i]-arr[i-2] and so on.
        if (arr[i] - arr[i - 1] < k)
        {
            // Assuming only positive numbers.
            maxSum += arr[i];
            maxSum += arr[i - 1];
 
            // When a match is found skip this pair
            --i;
        }
    }
 
    return maxSum;
}
 
// Driver code
int main()
{
    int arr[] = { 3, 5, 10, 15, 17, 12, 9 };
    int N = sizeof(arr) / sizeof(int);
 
    int K = 4;
    cout << maxSumPair(arr, N, K);
    return 0;
}


Java
// Java program to find maximum pair sum whose
// difference is less than K
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Method to return maximum sum we can get by
    // finding less than K difference pairs
    static int maxSumPairWithDifferenceLessThanK(int arr[],
                                                 int N,
                                                 int k)
    {
        int maxSum = 0;
 
        // Sort elements to ensure every i and i-1 is
        // closest possible pair
        Arrays.sort(arr);
 
        // To get maximum possible sum,
        // iterate from largest
        // to smallest, giving larger
        // numbers priority over
        // smaller numbers.
        for (int i = N - 1; i > 0; --i)
        {
            // Case I: Diff of arr[i] and arr[i-1] is less
            // then K, add to maxSum
            // Case II: Diff between arr[i] and arr[i-1] is
            // not less then K, move to next i
            // since with sorting we know, arr[i]-arr[i-1] <
            // arr[i]-arr[i-2] and so on.
            if (arr[i] - arr[i - 1] < k)
            {
                // Assuming only positive numbers.
                maxSum += arr[i];
                maxSum += arr[i - 1];
 
                // When a match is found skip this pair
                --i;
            }
        }
 
        return maxSum;
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        int arr[] = { 3, 5, 10, 15, 17, 12, 9 };
        int N = arr.length;
        int K = 4;
 
        System.out.println(
            maxSumPairWithDifferenceLessThanK(arr, N, K));
    }
}
 
// This code is contributed by vt_m.


Python3
# Python3 program to find maximum pair sum
# whose difference is less than K
 
# Method to return maximum sum we can
# get by finding less than K difference
# pairs
 
 
def maxSumPairWithDifferenceLessThanK(arr, N, k):
 
    maxSum = 0
 
    # Sort elements to ensure every i and
    # i-1 is closest possible pair
    arr.sort()
 
    # To get maximum possible sum, iterate
    # from largest to smallest, giving larger
    # numbers priority over smaller numbers.
    i = N - 1
    while (i > 0):
 
        # Case I: Diff of arr[i] and arr[i-1]
        #     is less then K, add to maxSum
        # Case II: Diff between arr[i] and
        #     arr[i-1] is not less then K,
        #     move to next i since with sorting
        #     we know, arr[i]-arr[i-1] < arr[i]-arr[i-2]
        #     and so on.
        if (arr[i] - arr[i - 1] < k):
 
            # Assuming only positive numbers.
            maxSum += arr[i]
            maxSum += arr[i - 1]
 
            # When a match is found skip this pair
            i -= 1
        i -= 1
 
    return maxSum
 
 
# Driver Code
arr = [3, 5, 10, 15, 17, 12, 9]
N = len(arr)
 
K = 4
print(maxSumPairWithDifferenceLessThanK(arr, N, K))
 
# This code is contributed by mits


C#
// C# program to find maximum pair sum whose
// difference is less than K
using System;
class GFG {
     
    // Method to return maximum sum we can get by
    // finding less than K difference pairs
    static int maxSumPairWithDifferenceLessThanK(int []arr,
                                               int N, int k)
    {
        int maxSum = 0;
     
        // Sort elements to ensure
        // every i and i-1 is closest
        // possible pair
        Array.Sort(arr);
     
        // To get maximum possible sum,
        // iterate from largest
        // to smallest, giving larger
        // numbers priority over
        // smaller numbers.
        for (int i = N-1; i > 0; --i)
        {
             
            /* Case I: Diff of arr[i] and
                       arr[i-1] is less then K,
                       add to maxSum
               Case II: Diff between arr[i] and
                        arr[i-1] is not less
                        then K, move to next i
                        since with sorting we
                        know, arr[i]-arr[i-1] <
                        arr[i]-arr[i-2] and
                        so on.*/
            if (arr[i] - arr[i-1] < k)
            {
                 
                // Assuming only positive numbers.
                maxSum += arr[i];
                maxSum += arr[i - 1];
     
                // When a match is found
                // skip this pair
                --i;
            }
        }
     
        return maxSum;
    }
 
    // Driver Code
    public static void Main ()
    {
        int []arr = {3, 5, 10, 15, 17, 12, 9};
        int N = arr.Length;
        int K = 4;
         
        Console.Write( maxSumPairWithDifferenceLessThanK(arr,
                                                         N, K));
    }
}
 
// This code is contributed by nitin mittal.


PHP
 0; --$i)
    {
        // Case I: Diff of arr[i] and arr[i-1]
        //           is less then K, add to maxSum
        // Case II: Diff between arr[i] and
        //            arr[i-1] is not less then K,
        //          move to next i since with sorting
        //          we know, arr[i]-arr[i-1] < arr[i]-arr[i-2]
        //            and so on.
        if ($arr[$i] - $arr[$i - 1] < $k)
        {
            // Assuming only positive numbers.
            $maxSum += $arr[$i];
            $maxSum += $arr[$i - 1];
 
            // When a match is found skip this pair
            --$i;
        }
    }
 
    return $maxSum;
}
 
// Driver Code
$arr = array(3, 5, 10, 15, 17, 12, 9);
$N = sizeof($arr);
 
$K = 4;
echo maxSumPairWithDifferenceLessThanK($arr, $N, $K);
 
// This code is contributed
// by Sach_Code   
?>


Javascript


输出
62

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

下面给出了 Amit Sane 提供的优化解决方案,

C++

// C++ program to find maximum pair sum whose
// difference is less than K
#include 
using namespace std;
 
// Method to return maximum sum we can get by
// finding less than K difference pairs
int maxSumPair(int arr[], int N, int k)
{
    int maxSum = 0;
 
    // Sort elements to ensure every i and i-1 is closest
    // possible pair
    sort(arr, arr + N);
 
    // To get maximum possible sum,
    // iterate from largest to
    // smallest, giving larger
    // numbers priority over smaller
    // numbers.
    for (int i = N - 1; i > 0; --i)
    {
        // Case I: Diff of arr[i] and arr[i-1]
        //  is less then K,add to maxSum      
        // Case II: Diff between arr[i] and arr[i-1] is not
        // less then K, move to next i since with
        // sorting we know, arr[i]-arr[i-1] <
        // rr[i]-arr[i-2] and so on.
        if (arr[i] - arr[i - 1] < k)
        {
            // Assuming only positive numbers.
            maxSum += arr[i];
            maxSum += arr[i - 1];
 
            // When a match is found skip this pair
            --i;
        }
    }
 
    return maxSum;
}
 
// Driver code
int main()
{
    int arr[] = { 3, 5, 10, 15, 17, 12, 9 };
    int N = sizeof(arr) / sizeof(int);
 
    int K = 4;
    cout << maxSumPair(arr, N, K);
    return 0;
}

Java

// Java program to find maximum pair sum whose
// difference is less than K
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Method to return maximum sum we can get by
    // finding less than K difference pairs
    static int maxSumPairWithDifferenceLessThanK(int arr[],
                                                 int N,
                                                 int k)
    {
        int maxSum = 0;
 
        // Sort elements to ensure every i and i-1 is
        // closest possible pair
        Arrays.sort(arr);
 
        // To get maximum possible sum,
        // iterate from largest
        // to smallest, giving larger
        // numbers priority over
        // smaller numbers.
        for (int i = N - 1; i > 0; --i)
        {
            // Case I: Diff of arr[i] and arr[i-1] is less
            // then K, add to maxSum
            // Case II: Diff between arr[i] and arr[i-1] is
            // not less then K, move to next i
            // since with sorting we know, arr[i]-arr[i-1] <
            // arr[i]-arr[i-2] and so on.
            if (arr[i] - arr[i - 1] < k)
            {
                // Assuming only positive numbers.
                maxSum += arr[i];
                maxSum += arr[i - 1];
 
                // When a match is found skip this pair
                --i;
            }
        }
 
        return maxSum;
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        int arr[] = { 3, 5, 10, 15, 17, 12, 9 };
        int N = arr.length;
        int K = 4;
 
        System.out.println(
            maxSumPairWithDifferenceLessThanK(arr, N, K));
    }
}
 
// This code is contributed by vt_m.

蟒蛇3

# Python3 program to find maximum pair sum
# whose difference is less than K
 
# Method to return maximum sum we can
# get by finding less than K difference
# pairs
 
 
def maxSumPairWithDifferenceLessThanK(arr, N, k):
 
    maxSum = 0
 
    # Sort elements to ensure every i and
    # i-1 is closest possible pair
    arr.sort()
 
    # To get maximum possible sum, iterate
    # from largest to smallest, giving larger
    # numbers priority over smaller numbers.
    i = N - 1
    while (i > 0):
 
        # Case I: Diff of arr[i] and arr[i-1]
        #     is less then K, add to maxSum
        # Case II: Diff between arr[i] and
        #     arr[i-1] is not less then K,
        #     move to next i since with sorting
        #     we know, arr[i]-arr[i-1] < arr[i]-arr[i-2]
        #     and so on.
        if (arr[i] - arr[i - 1] < k):
 
            # Assuming only positive numbers.
            maxSum += arr[i]
            maxSum += arr[i - 1]
 
            # When a match is found skip this pair
            i -= 1
        i -= 1
 
    return maxSum
 
 
# Driver Code
arr = [3, 5, 10, 15, 17, 12, 9]
N = len(arr)
 
K = 4
print(maxSumPairWithDifferenceLessThanK(arr, N, K))
 
# This code is contributed by mits

C#

// C# program to find maximum pair sum whose
// difference is less than K
using System;
class GFG {
     
    // Method to return maximum sum we can get by
    // finding less than K difference pairs
    static int maxSumPairWithDifferenceLessThanK(int []arr,
                                               int N, int k)
    {
        int maxSum = 0;
     
        // Sort elements to ensure
        // every i and i-1 is closest
        // possible pair
        Array.Sort(arr);
     
        // To get maximum possible sum,
        // iterate from largest
        // to smallest, giving larger
        // numbers priority over
        // smaller numbers.
        for (int i = N-1; i > 0; --i)
        {
             
            /* Case I: Diff of arr[i] and
                       arr[i-1] is less then K,
                       add to maxSum
               Case II: Diff between arr[i] and
                        arr[i-1] is not less
                        then K, move to next i
                        since with sorting we
                        know, arr[i]-arr[i-1] <
                        arr[i]-arr[i-2] and
                        so on.*/
            if (arr[i] - arr[i-1] < k)
            {
                 
                // Assuming only positive numbers.
                maxSum += arr[i];
                maxSum += arr[i - 1];
     
                // When a match is found
                // skip this pair
                --i;
            }
        }
     
        return maxSum;
    }
 
    // Driver Code
    public static void Main ()
    {
        int []arr = {3, 5, 10, 15, 17, 12, 9};
        int N = arr.Length;
        int K = 4;
         
        Console.Write( maxSumPairWithDifferenceLessThanK(arr,
                                                         N, K));
    }
}
 
// This code is contributed by nitin mittal.

PHP

 0; --$i)
    {
        // Case I: Diff of arr[i] and arr[i-1]
        //           is less then K, add to maxSum
        // Case II: Diff between arr[i] and
        //            arr[i-1] is not less then K,
        //          move to next i since with sorting
        //          we know, arr[i]-arr[i-1] < arr[i]-arr[i-2]
        //            and so on.
        if ($arr[$i] - $arr[$i - 1] < $k)
        {
            // Assuming only positive numbers.
            $maxSum += $arr[$i];
            $maxSum += $arr[$i - 1];
 
            // When a match is found skip this pair
            --$i;
        }
    }
 
    return $maxSum;
}
 
// Driver Code
$arr = array(3, 5, 10, 15, 17, 12, 9);
$N = sizeof($arr);
 
$K = 4;
echo maxSumPairWithDifferenceLessThanK($arr, $N, $K);
 
// This code is contributed
// by Sach_Code   
?>

Javascript


输出
62

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程