📌  相关文章
📜  数组中三元组的不同对之间的绝对绝对值的最大和

📅  最后修改于: 2021-05-17 16:10:10             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是找到该数组中所有不同对的三元组之间的绝对绝对值的最大和。

例子:

方法:解决给定问题的想法是按升序对数组进行排序,并找到数组的前两个元素对之间的绝对差之和。请按照以下步骤解决问题:

  • 初始化一个变量,例如sum ,以存储最大可能的和。
  • 以升序对给定数组arr []进行排序。
  • 找到数组的第一个和最后两个元素对之间的差之和,即sum =(arr [N – 2] – arr [0])+(arr [N – 1] – arr [0]) +(arr [N – 2] – arr [N – 1])
  • 完成上述步骤后,打印总和的值作为结果

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum sum of
// absolute differences between
// distinct pairs of triplet in array
void maximumSum(int arr[], int N)
{
    // Stores the maximum sum
    int sum;
 
    // Sort the array in
    // ascending order
    sort(arr, arr + N);
 
    // Sum of differences between
    // pairs of the triplet
    sum = (arr[N - 1] - arr[0])
          + (arr[N - 2] - arr[0])
          + (arr[N - 1] - arr[N - 2]);
 
    // Print the sum
    cout << sum;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 3, 4, 2 };
    int N = sizeof(arr) / sizeof(arr[0]);
    maximumSum(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
  // Function to find the maximum sum of
  // absolute differences between
  // distinct pairs of triplet in array
  static void maximumSum(int[] arr, int N)
  {
 
    // Stores the maximum sum
    int sum;
 
    // Sort the array in
    // ascending order
    Arrays.sort(arr);
 
    // Sum of differences between
    // pairs of the triplet
    sum = (arr[N - 1] - arr[0]) + (arr[N - 2] - arr[0])
      + (arr[N - 1] - arr[N - 2]);
 
    // Print the sum
    System.out.println(sum);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int[] arr = { 1, 3, 4, 2 };
    int N = arr.length;
    maximumSum(arr, N);
  }
}
 
// This code is contributed by susmitakundugoaldanga.


Python3
# Python program for the above approach
 
# Function to find the maximum sum of
# absolute differences between
# distinct pairs of triplet in array
def maximumSum(arr, N):
   
    # Stores the maximum sum
    sum = 0
     
    # Sort the array in
    # ascending order
    arr.sort()
     
    # Sum of differences between
    # pairs of the triplet
    sum = (arr[N - 1] - arr[0]) + (arr[N - 2] - arr[0]) + (arr[N - 1] - arr[N - 2]);
 
    # Print the sum
    print(sum)
 
# Driver Code
arr = [ 1, 3, 4, 2 ]
N = len(arr)
maximumSum(arr, N)
 
# This code is contributed by rohitsingh07052.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
    // Function to find the maximum sum of
    // absolute differences between
    // distinct pairs of triplet in array
    static void maximumSum(int[] arr, int N)
    {
       
        // Stores the maximum sum
        int sum;
 
        // Sort the array in
        // ascending order
        Array.Sort(arr);
 
        // Sum of differences between
        // pairs of the triplet
        sum = (arr[N - 1] - arr[0]) + (arr[N - 2] - arr[0])
              + (arr[N - 1] - arr[N - 2]);
 
        // Print the sum
        Console.Write(sum);
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr = { 1, 3, 4, 2 };
        int N = arr.Length;
        maximumSum(arr, N);
    }
}
 
// This code is contributed by chitranayal.


Javascript


输出:
6

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