给定大小为N的整数数组arr [] ,任务是计算具有最小绝对差的不同对的总数。
例子:
Input: arr[] = {4, 2, 1, 3}
Output: 3
Explanation:
The minimum absolute difference between the pairs {1, 2}, {2, 3}, {3, 4} is 1.
Input: arr[] = {1, 3, 8, 10, 15}
Output: 2
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 the count of all
// pairs having minimal absolute difference
int numberofpairs(int arr[], int N)
{
// Stores the count of pairs
int answer = 0;
// Sort the array
sort(arr, arr + N);
// Stores the minimum difference
// between adjacent pairs
int minDiff = INT_MAX;
for (int i = 0; i < N - 1; i++)
// Update the minimum
// difference between pairs
minDiff = min(minDiff,
arr[i + 1] - arr[i]);
for (int i = 0; i < N - 1; i++) {
if (arr[i + 1] - arr[i] == minDiff)
// Increase count of
// pairs with difference
// equal to that of
// minimum difference
answer++;
}
// Return the final count
return answer;
}
// Driver Code
int main()
{
// Given array arr[]
int arr[] = { 4, 2, 1, 3 };
int N = (sizeof arr) / (sizeof arr[0]);
// Function Call
cout << numberofpairs(arr, N) << "\n";
return 0;
}
Java
// Java program for the above
import java.util.Arrays;
class GFG{
// Function to return the count of all
// pairs having minimal absolute difference
static int numberofpairs(int []arr, int N)
{
// Stores the count of pairs
int answer = 0;
// Sort the array
Arrays.sort(arr);
// Stores the minimum difference
// between adjacent pairs
int minDiff = 10000000;
for (int i = 0; i < N - 1; i++)
// Update the minimum
// difference between pairs
minDiff = Math.min(minDiff,
arr[i + 1] - arr[i]);
for (int i = 0; i < N - 1; i++)
{
if (arr[i + 1] - arr[i] == minDiff)
// Increase count of
// pairs with difference
// equal to that of
// minimum difference
answer++;
}
// Return the final count
return answer;
}
// Driver Code
public static void main(String[] args)
{
// Given array arr[]
int arr[] = { 4, 2, 1, 3 };
int N = arr.length;
// Function Call
System.out.print(numberofpairs(arr, N));
}
}
// This code is contributed by rock_cool
Python3
# Python3 program to implement
# the above approach
# Function to return the count of all
# pairs having minimal absolute difference
def numberofpairs(arr, N):
# Stores the count of pairs
answer = 0
# Sort the array
arr.sort()
# Stores the minimum difference
# between adjacent pairs
minDiff = 10000000
for i in range(0, N - 1):
# Update the minimum
# difference between pairs
minDiff = min(minDiff,
arr[i + 1] - arr[i])
for i in range(0, N - 1):
if arr[i + 1] - arr[i] == minDiff:
# Increase count of pairs
# with difference equal to
# that of minimum difference
answer += 1
# Return the final count
return answer
# Driver code
if __name__ == '__main__':
# Given array arr[]
arr = [ 4, 2, 1, 3 ]
N = len(arr)
# Function call
print(numberofpairs(arr,N))
# This code is contributed by virusbuddah_
C#
// C# program for the above approach
using System;
class GFG{
// Function to return the count of all
// pairs having minimal absolute difference
static int numberofpairs(int []arr, int N)
{
// Stores the count of pairs
int answer = 0;
// Sort the array
Array.Sort(arr);
// Stores the minimum difference
// between adjacent pairs
int minDiff = 10000000;
for(int i = 0; i < N - 1; i++)
// Update the minimum
// difference between pairs
minDiff = Math.Min(minDiff,
arr[i + 1] -
arr[i]);
for(int i = 0; i < N - 1; i++)
{
if (arr[i + 1] - arr[i] == minDiff)
// Increase count of
// pairs with difference
// equal to that of
// minimum difference
answer++;
}
// Return the final count
return answer;
}
// Driver Code
public static void Main(String[] args)
{
// Given array arr[]
int []arr = { 4, 2, 1, 3 };
int N = arr.Length;
// Function Call
Console.Write(numberofpairs(arr, N));
}
}
// This code is contributed by shivanisinghss2110
Javascript
输出:
3
时间复杂度: O(N * log N)
辅助空间: O(1)