📌  相关文章
📜  找到对 i, j 使得 |A[i]−A[j]|与它们与任何 Array 元素的差异之和相同

📅  最后修改于: 2022-05-13 01:56:10.477000             🧑  作者: Mango

找到对 i, j 使得 |A[i]−A[j]|与它们与任何 Array 元素的差异之和相同

给定一个具有N个非负整数的数组A[] ,找到一对索引i 和 j ,使得它们之间的绝对差值等于与任何其他数组元素的差值之和,即| A[i] - A[k] | + | A[k]− A[j] | = | A[i] - A[j] |,其中k可以是任何索引。

例子:

方法:这个问题可以通过以下数学观察来解决:

根据以上观察,很明显 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)