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

📅  最后修改于: 2021-09-06 06:24:42             🧑  作者: Mango

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

例子:

处理方法:贪心即可解决问题。请按照以下步骤解决问题:

  1. 遍历数组。
  2. 按升序对数组进行排序。
  3. 使用变量i在索引[0, N – 3] 上遍历数组。对于每个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.


Javascript


输出:
1

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

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