按绝对差最小的排序顺序查找数组中的所有对
给定一个大小为N的整数数组arr[] ,任务是找到所有具有最小绝对差的不同对并按升序打印它们。
例子:
Input: arr[] = {4, 2, 1, 3}
Output: {1, 2}, {2, 3}, {3, 4}
Explanation: The minimum absolute difference between pairs is 1.
Input: arr[] = {1, 3, 8, 10, 15}
Output: {1, 3}, {8, 10}
Explanation: The minimum absolute difference between the pairs {1, 3}, {8, 10} is 2.
方法:想法是考虑排序数组的相邻元素的绝对差异。请按照以下步骤解决问题:
- 对给定的数组 arr[] 进行排序。
- 比较排序数组中的所有相邻对,并找到所有相邻对之间的最小绝对差。
- 最后,打印所有差值等于最小绝对差值的相邻对。
下面是上述代码的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to return all
// pairs having minimal absolute difference
vector > minAbsDiffPairs(vector& arr)
{
vector > ans;
int n = arr.size();
// Sort the array
sort(arr.begin(), arr.end());
// Stores the minimal absolute difference
int minDiff = INT_MAX;
for (int i = 0; i < n - 1; i++)
minDiff = min(minDiff, abs(arr[i] - arr[i + 1]));
for (int i = 0; i < n - 1; i++) {
vector pair;
if (abs(arr[i] - arr[i + 1]) == minDiff) {
pair.push_back(min(arr[i], arr[i + 1]));
pair.push_back(max(arr[i], arr[i + 1]));
ans.push_back(pair);
}
}
return ans;
}
// Driver Code
int main()
{
vector arr = { 4, 2, 1, 3 };
int N = (sizeof arr) / (sizeof arr[0]);
vector > pairs = minAbsDiffPairs(arr);
// Print all pairs
for (auto v : pairs)
cout << v[0] << " " << v[1] << endl;
return 0;
}
Java
// Java program for the above approach
import java.util.ArrayList;
import java.util.Collections;
class GFG
{
// Function to return all
// pairs having minimal absolute difference
static ArrayList> minAbsDiffPairs(ArrayList arr) {
ArrayList> ans = new ArrayList>();
int n = arr.size();
// Sort the array
Collections.sort(arr);
// Stores the minimal absolute difference
int minDiff = Integer.MAX_VALUE;
for (int i = 0; i < n - 1; i++)
minDiff = Math.min(minDiff, Math.abs(arr.get(i) - arr.get(i + 1)));
for (int i = 0; i < n - 1; i++) {
ArrayList pair = new ArrayList();
if (Math.abs(arr.get(i) - arr.get(i + 1)) == minDiff) {
pair.add(Math.min(arr.get(i), arr.get(i + 1)));
pair.add(Math.max(arr.get(i), arr.get(i + 1)));
ans.add(pair);
}
}
return ans;
}
// Driver Code
public static void main(String args[]) {
ArrayList arr = new ArrayList();
arr.add(4);
arr.add(2);
arr.add(1);
arr.add(3);
ArrayList> pairs = minAbsDiffPairs(arr);
// Print all pairs
// System.out.println(pairs);
for (ArrayList v : pairs) {
for (int w : v)
System.out.print(w + " ");
System.out.println("");
}
}
}
// This code is contributed by saurabh_jaiswal.
Python3
# Python3 program for the above approach
import math as Math
# Function to return all pairs having
# minimal absolute difference
def minAbsDiffPairs(arr):
ans = []
n = len(arr)
# Sort the array
arr.sort()
# Stores the minimal absolute difference
minDiff = 10 ** 9
for i in range(n - 1):
minDiff = min(minDiff, Math.fabs(arr[i] -
arr[i + 1]))
for i in range(n - 1):
pair = []
if (Math.fabs(arr[i] - arr[i + 1]) == minDiff):
pair.append(min(arr[i], arr[i + 1]))
pair.append(max(arr[i], arr[i + 1]))
ans.append(pair)
return ans
# Driver Code
arr = [ 4, 2, 1, 3 ]
N = len(arr)
pairs = minAbsDiffPairs(arr)
# Print all pairs
for v in pairs:
print(f"{v[0]} {v[1]}")
# This code is contributed by gfgking
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to return all
// pairs having minimal Absolute difference
static List> minAbsDiffPairs(List arr) {
List> ans = new List>();
int n = arr.Count;
// Sort the array
arr.Sort();
// Stores the minimal Absolute difference
int minDiff = int.MaxValue;
for (int i = 0; i < n - 1; i++)
minDiff = Math.Min(minDiff, Math.Abs(arr[i] - arr[i + 1]));
for (int i = 0; i < n - 1; i++) {
List pair = new List();
if (Math.Abs(arr[i] - arr[i + 1]) == minDiff) {
pair.Add(Math.Min(arr[i], arr[i + 1]));
pair.Add(Math.Max(arr[i], arr[i + 1]));
ans.Add(pair);
}
}
return ans;
}
// Driver Code
public static void Main()
{
List arr = new List();
arr.Add(4);
arr.Add(2);
arr.Add(1);
arr.Add(3);
List> pairs = minAbsDiffPairs(arr);
// Print all pairs
// System.out.println(pairs);
foreach (List v in pairs)
{
foreach (int w in v)
Console.Write(w + " ");
Console.WriteLine("");
}
}
}
// This code is contributed by saurabh_jaiswal.
Javascript
输出
1 2
2 3
3 4
时间复杂度:O(NlogN)
辅助空间:O(N)