📜  在给定对中找到具有更大平均值的对索引

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

在给定对中找到具有更大平均值的对索引

给定一个大小为N的对数组arr[] ,其中所有对的第一个值是不同的。对于给定数组的每一对,找到另一对的索引,其平均值刚好大于这个。

注意:两个数字 a 和 b 的平均值定义为底 (a + b) / 2。

例子:

方法:这个问题可以用二分查找来解决。请按照以下步骤操作:

  • 根据平均值对数组对进行排序。
  • 对每个元素应用二进制搜索以找到平均值刚好大于这个值的对。
  • 要跟踪索引,请使用哈希映射或无序映射,因为第一个值在所有对中都是不同的。

下面是上述方法的实现:

C++
// C++ program to implement above approach
#include 
using namespace std;
 
// Applying custom lower bound
// on average values
int lbound(vector >& arr,
           int& target)
{
    // Initializing low and high variables
    int low = 0, high = (int)arr.size() - 1;
 
    // ans keeps the track of desired index
    int ans = -1;
 
    // Applying binary search
    while (low <= high) {
        int mid = (low + high) / 2;
 
        int avg = (arr[mid].first
                   + arr[mid].second)
                  / 2;
        if (avg <= target)
            low = mid + 1;
        else {
            high = mid - 1;
            ans = mid;
        }
    }
 
    if (ans == -1)
        return -1;
 
    // Return the ans
    int avg = (arr[ans].first
               + arr[ans].second)
              / 2;
 
    if (avg > target)
        return ans;
    return -1;
}
 
// Compare function
bool compare(pair& a,
             pair& b)
{
    int val1 = (a.first + a.second) / 2;
    int val2 = (b.first + b.second) / 2;
 
    return val1 <= val2;
}
 
// Function to find indices of desired pair
// for each element of the array of pairs
void findPair(vector >& arr,
              int N)
{
    // Declaring an unordered map
    // to keep the track of
    // indices since the order
    // is going to be changed
    unordered_map lookUp;
 
    // Iterating over the given vector
    for (int i = 0; i < N; i++) {
        lookUp[arr[i].first] = i;
    }
 
    // Sorting the given vector of pairs
    // by passing a custom comparator
    sort(arr.begin(), arr.end(), compare);
 
    // Declaring ans vector to store
    // the final answer
    vector ans(N);
 
    for (int i = 0; i < N; i++) {
 
        // Average value
        int target = (arr[i].first
                      + arr[i].second)
                     / 2;
 
        // Calling lbound() function
        int ind = lbound(arr, target);
 
        // If no such pair exists
        if (ind == -1)
            ans[lookUp[arr[i].first]] = -1;
 
        // If such a pair exists
        else
            ans[lookUp[arr[i].first]]
                = lookUp[arr[ind].first];
    }
 
    // Print the final array
    for (auto x : ans)
        cout << x << ' ';
}
 
// Driver code
int main()
{
    // Initializing a vector of pairs
    vector > arr
        = { { 2, 3 }, { 1, 3 }, { 3, 4 } };
 
    // Size of the vector
    int N = arr.size();
 
    findPair(arr, N);
    return 0;
}


C#
// C# program to implement above approach
 
using System;
using System.Linq;
using System.Collections.Generic;
 
public class GFG {
  public class pair {
    public int first, second;
 
    public pair(int first, int second) {
      this.first = first;
      this.second = second;
    }
  }
 
  static List arr;
  static int target;
 
  // Applying custom lower bound
  // on average values
  static int lbound()
  {
 
    // Initializing low and high variables
    int low = 0, high = arr.Count - 1;
 
    // ans keeps the track of desired index
    int ans = -1;
 
    // Applying binary search
    while (low <= high) {
      int mid = (low + high) / 2;
 
      int a = (arr[mid].first + arr[mid].second) / 2;
      if (a <= target)
        low = mid + 1;
      else {
        high = mid - 1;
        ans = mid;
      }
    }
 
    if (ans == -1)
      return -1;
 
    // Return the ans
    int avg = (arr[ans].first + arr[ans].second) / 2;
 
    if (avg > target)
      return ans;
    return -1;
  }
 
  // Compare function
 
  // Function to find indices of desired pair
  // for each element of the array of pairs
  static void findPair(int N)
  {
 
    // Declaring an unordered map
    // to keep the track of
    // indices since the order
    // is going to be changed
    Dictionary lookUp = new Dictionary();
 
    // Iterating over the given vector
    for (int i = 0; i < N; i++) {
      lookUp[arr[i].first] = i;
    }
 
    // Sorting the given vector of pairs
    // by passing a custom comparator
    arr.Sort((a, b) => ((a.first + a.second) / 2)-((b.first + b.second) / 2));
 
    // Declaring ans vector to store
    // the readonly answer
    List ans = new List();
 
    for (int i = 0; i < N; i++) {
      ans.Add(0);
       
      // Average value
      target = (arr[i].first
                + arr[i].second)
        / 2;
 
      // Calling lbound() function
      int ind = lbound();
 
      // If no such pair exists
      if (ind == -1)
        ans[lookUp[arr[i].first]] = -1;
 
      // If such a pair exists
      else
        ans[lookUp[arr[i].first]]
        = lookUp[arr[ind].first];
    }
 
    // Print the readonly array
    foreach ( int x in ans)
      Console.Write(x + " ");
  }
 
  // Driver code
  public static void Main(String[] args)
  {
     
    // Initializing a vector of pairs
    arr = new List();
    arr.Add(  new pair(2, 3));
    arr.Add(new pair(1, 3));
    arr.Add(new pair(3, 4));
     
    // Size of the vector
    int N = arr.Count;
 
    findPair(3);
  }
}
 
// This code is contributed by Rajput-Ji


Javascript


输出
2 2 -1 


输出
2 2 -1 

时间复杂度: O(N * log(N) )
辅助空间: 在)