给定一个由正整数组成的数组A[] ,任务是找到|A[x] – A[y]|的最小值+ |A[y] – A[z]|来自数组的任何三元组(A[x], A[y], A[z]) 。
例子:
Input: A[] = { 1, 1, 2, 3 }
Output: 1
Explanation:
For x = 0, y = 1, z = 2
|A[x] – A[y]| + |A[y] – A[z]| = 0 + 1 = 1, which is maximum possible
Input : A[] = { 1, 1, 1 }
Output : 0
处理方法:贪心即可解决问题。请按照以下步骤解决问题:
- 遍历数组。
- 按升序对数组进行排序。
- 使用变量i在索引[0, N – 3] 上遍历数组。对于每个第i个索引,设置x = i, y = i + 1, z = i + 2
- 计算三元组(x, y, z)的总和。
- 更新可能的最小总和。
- 打印获得的最小和。
下面是上述方法的实现:
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 minimum 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 minimum 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 minimum 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 minimum 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)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。