📌  相关文章
📜  数组中三元组对之间的绝对差的最小总和

📅  最后修改于: 2021-04-17 17:47:11             🧑  作者: Mango

给定一个包含正整数的数组A [] ,任务是找到| A [x] – A [y] |的最小值+ | A [y] – A [z] |数组中的任何三元组(A [x],A [y],A [z])

例子:

方法:可以贪婪地解决问题。请按照以下步骤解决问题:

  1. 遍历数组。
  2. 以升序对数组进行排序。
  3. 在索引[0,N – 3]上使用变量i遍历数组。对于每个i索引,设置x = i,y = i + 1,z = i + 2
  4. 计算三元组(x,y,z)的总和。
  5. 更新可能的最小和。
  6. 打印获得的最小金额。

下面是上述方法的实现:

C++
// C++ Program for the above approach
 
#include 
using namespace std;
 
// Function to find minimum
// sum of absolute differences
// of pairs of a triplet
int minimum_sum(int A[], int N)
{
    // Sort the array
    sort(A, A + N);
 
    // Stores the minuimum sum
    int sum = INT_MAX;
 
    // Traverse the array
    for (int i = 0; i <= N - 3; i++) {
 
        // Update the minimum sum
        sum = min(sum,
                  abs(A[i] - A[i + 1]) +
                  abs(A[i + 1] - A[i + 2]));
    }
 
    // Print the minimum sum
    cout << sum;
}
 
// Driver Code
int main()
{
 
    // Input
    int A[] = { 1, 1, 2, 3 };
    int N = sizeof(A) / sizeof(A[0]);
 
    // Function call to find minimum
    // sum of absolute differences
    // of pairs in a triplet
    minimum_sum(A, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
   
// Function to find minimum
// sum of absolute differences
// of pairs of a triplet
static int minimum_sum(int []A, int N)
{
   
    // Sort the array
    Arrays.sort(A);
 
    // Stores the minuimum sum
 
    int sum = 2147483647;
 
    // Traverse the array
    for (int i = 0; i <= N - 3; i++) {
 
        // Update the minimum sum
        sum = Math.min(sum,Math.abs(A[i] - A[i + 1]) + Math.abs(A[i + 1] - A[i + 2]));
    }
 
    // Print the minimum sum
    return sum;
}
 
// Driver Code
public static void main(String[] args)
{
   
    // Input
    int []A = { 1, 1, 2, 3 };
    int N = A.length;
 
    // Function call to find minimum
    // sum of absolute differences
    // of pairs in a triplet
    System.out.print(minimum_sum(A, N));
}
}
 
// This code is contributed by splevel62.


Python3
# Python 3 Program for the above approach
import sys
 
# Function to find minimum
# sum of absolute differences
# of pairs of a triplet
def minimum_sum(A, N):
   
    # Sort the array
    A.sort(reverse = False)
 
    # Stores the minuimum sum
    sum = sys.maxsize
 
    # Traverse the array
    for i in range(N - 2):
       
        # Update the minimum sum
        sum = min(sum, abs(A[i] - A[i + 1]) + abs(A[i + 1] - A[i + 2]))
 
    # Print the minimum sum
    print(sum)
 
# Driver Code
if __name__ == '__main__':
   
    # Input
    A = [1, 1, 2, 3]
    N = len(A)
 
    # Function call to find minimum
    # sum of absolute differences
    # of pairs in a triplet
    minimum_sum(A, N)
     
    # This code is contributed by ipg2016107


C#
// C# Program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
    
// Function to find minimum
// sum of absolute differences
// of pairs of a triplet
static int minimum_sum(int []A, int N)
{
   
    // Sort the array
    Array.Sort(A);
 
    // Stores the minuimum sum
 
    int sum = 2147483647;
 
    // Traverse the array
    for (int i = 0; i <= N - 3; i++) {
 
        // Update the minimum sum
        sum = Math.Min(sum,Math.Abs(A[i] - A[i + 1]) + Math.Abs(A[i + 1] - A[i + 2]));
    }
 
    // Print the minimum sum
    return sum;
}
 
// Driver Code
public static void Main()
{
 
    // Input
    int []A = { 1, 1, 2, 3 };
    int N = A.Length;
 
    // Function call to find minimum
    // sum of absolute differences
    // of pairs in a triplet
    Console.WriteLine(minimum_sum(A, N));
}
}
 
// This code is contributed by bgangwar59.


输出:
1

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