找到对 i, j 使得 |A[i]−A[j]|与它们与任何 Array 元素的差异之和相同
给定一个具有N个非负整数的数组A[] ,找到一对索引i 和 j ,使得它们之间的绝对差值等于与任何其他数组元素的差值之和,即| A[i] - A[k] | + | A[k]− A[j] | = | A[i] - A[j] |,其中k可以是任何索引。
例子:
Input: N = 3, A[] = {2, 7, 5}
Output: 0 1
Explanation:
For k = 0:
|A[0] – A[0]| + |A[0] – A[1]|
= |2 − 2| + |2 – 7| = 0 + 5 = 5
= |A[0] – A[1]|
For k = 1:
|A[0] – A[1]| + |A[1] – A[1]|
= |2 − 7| + |7 – 7| = 5 + 0 = 5
= |A[0] – A[1]|
For k = 2:
|A[0] – A[2]| + |A[2] – A[1]|
= |2 − 5| + |5 – 7| = 3 + 2 = 5
= |A[0] – A[1]|
Input: N = 4, arr[] = {5, 9, 1, 3}
Output: 1 2
Explanation:
For k = 0:
|A[1] – A[0]| + |A[0] – A[2]|
= |9 − 5| + |5 – 1| = 4 + 4 = 8
= |A[1] – A[2]|
For k = 1:
|A[1] – A[1]| + |A[1] – A[2]|
= |9 − 9| + |9 – 1| = 0 + 8 = 8
= |A[1] – A[2]|
For k = 2:
|A[1] – A[2]| + |A[2] – A[2]|
= |9 − 1| + |1 – 1| = 8 + 0 = 8
= |A[1] – A[2]|
For k = 3:
|A[1] – A[3]| + |A[3] – A[2]|
= |9 − 3| + |3 – 1| = 6 + 2 = 8
= |A[1] – A[2]|
方法:这个问题可以通过以下数学观察来解决:
Depending on the relation between A[i], A[j] and A[k], the inequality can be written in the following 4 ways:
When A[i] ≥ A[k] ≥ A[j]:
A[i] − A[k] + A[k]− A[j] = A[i] − A[j]
=> A[i] – A[j] = A[i] − A[j]
When A[k] ≥ A[i], A[k] ≥ A[j]:
A[k] − A[i] + A[k]− A[j] = |A[i] − A[j]|
=> 2*A[k] – A[i] – A[j] = |A[i] − A[j]|
When A[i] ≥ A[k], A[j] ≥ A[k]:
A[i] − A[k] – A[k]+ A[j] = |A[i] − A[j]|
=> A[i] + A[j] – 2*A[k] = |A[i] − A[j]|
When A[j] ≥ A[k] ≥ A[i]:
– A[i] + A[k] – A[k] + A[j] = – A[i] + A[j]
=> A[j] – A[i] = A[j] − A[i].
From the above equations, it is clear that if value of A[i] and A[j] are not the extreme values of the array then the probability of the equation being satisfied depends on the value of A[k] and will not hold true when A[k] lies outside the range of [A[i], A[j]].
根据以上观察,很明显 A[i] 和 A[j] 的值应该是数组元素中的最大值和最小值。请按照以下步骤解决问题:
- 从k = 0 到 N-1遍历数组:
- 如果 A[k] 大于 A[i],则更新最大元素的索引(比如i )。
- 如果 A[k] 小于 A[j],则更新最小元素(比如j )的索引。
- 返回对(i, j)作为答案。
下面是上述方法的实现。
C++
// C++ program for aboe approach
#include
using namespace std;
// Function to find the pair
pair findPair(int N,
vector vec)
{
// Maximum element of the array
int maxi = *max_element(vec.begin(),
vec.end());
// Minimum element of the array
int mini = *min_element(vec.begin(),
vec.end());
int idx1 = 0;
int idx2 = 0;
// Loop to find index of maximum element
for (int i = 0; i < N; i++) {
if (vec[i] == maxi)
idx1 = i;
}
// Loop to find index of minimum element
for (int i = 0; i < N; i++) {
if (vec[i] == mini)
idx2 = i;
}
return { idx2, idx1 };
}
// Driver code
int main()
{
int N = 3;
vector vec{ 2, 7, 5 };
// Function call
pair ans = findPair(N, vec);
cout << ans.first << " " << ans.second;
return 0;
}
Java
// Java program for above approach
import java.io.*;
class GFG
{
// Function to find the pair
public static int[] findPair(int N, int vec[])
{
// Maximum element of the array
int maxi = Integer.MIN_VALUE;
for (int i = 0; i < N; i++) {
maxi = Math.max(maxi, vec[i]);
}
// Minimum element of the array
int mini = Integer.MAX_VALUE;
for (int i = 0; i < N; i++) {
mini = Math.min(mini, vec[i]);
}
int idx1 = 0;
int idx2 = 0;
// Loop to find index of maximum element
for (int i = 0; i < N; i++) {
if (vec[i] == maxi)
idx1 = i;
}
// Loop to find index of minimum element
for (int i = 0; i < N; i++) {
if (vec[i] == mini)
idx2 = i;
}
int res[] = { idx2, idx1 };
return res;
}
public static void main(String[] args)
{
int N = 3;
int vec[] = { 2, 7, 5 };
// Function call
int ans[] = findPair(N, vec);
System.out.print(ans[0] + " " + ans[1]);
}
}
// This code is contributed by Rohit Pradhan
Python3
# Python program for aboe approach
# Function to find the pair
def findPair(N, vec):
# Maximum element of the array
maxi = max(vec)
# Minimum element of the array
mini = min(vec)
idx1 = 0
idx2 = 0
# Loop to find index of maximum element
for i in range(N):
if (vec[i] == maxi):
idx1 = i
# Loop to find index of minimum element
for i in range(N):
if (vec[i] == mini):
idx2 = i
return [idx2, idx1]
# Driver code
N = 3
vec = [2, 7, 5]
# Function call
ans = findPair(N, vec)
print(f"{ans[0]} {ans[1]}")
# This code is contributed by shinjanpatra
C#
// C# program for above approach
using System;
public class GFG{
// Function to find the pair
public static int[] findPair(int N, int[] vec)
{
// Maximum element of the array
int maxi = Int32.MinValue;
for (int i = 0; i < N; i++) {
maxi = Math.Max(maxi, vec[i]);
}
// Minimum element of the array
int mini = Int32.MaxValue;
for (int i = 0; i < N; i++) {
mini = Math.Min(mini, vec[i]);
}
int idx1 = 0;
int idx2 = 0;
// Loop to find index of maximum element
for (int i = 0; i < N; i++) {
if (vec[i] == maxi)
idx1 = i;
}
// Loop to find index of minimum element
for (int i = 0; i < N; i++) {
if (vec[i] == mini)
idx2 = i;
}
int[] res = { idx2, idx1 };
return res;
}
static public void Main (){
int N = 3;
int[] vec = { 2, 7, 5 };
// Function call
int[] ans = findPair(N, vec);
Console.Write(ans[0] + " " + ans[1]);
}
}
// This code is contributed by hrithikgarg03188.
Javascript
0 1
时间复杂度: O(N)
辅助空间: O(1)