📜  查询以返回第L个最小数字与第R个最小数字之间的绝对差

📅  最后修改于: 2021-06-25 20:04:02             🧑  作者: Mango

给定一个由N个唯一元素和Q个查询组成的数组arr [] 。每个查询由两个整数LR组成。的任务是打印为L最小索引和r最小的元素之间的绝对差。

例子:

方法:可以按照以下步骤解决上述问题。

  • 将元素及其索引存储在成对的数组中。
  • 根据第一个元素对数组进行排序。
  • 对于每个查询,答案都是abs(arr [l – 1] .second – arr [r – 1] .second)

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Fucntion to return the result
// for a particular query
int answerQuery(pair arr[], int l, int r)
{
    // Get the difference between the indices of
    // L-th and the R-th smallest element
    int answer = abs(arr[l - 1].second - arr[r - 1].second);
  
    // Return the answer
    return answer;
}
  
// Function that performs all the queries
void solveQueries(int a[], int n, int q[][2], int m)
{
  
    // Store the array numbers
    // and their indices
    pair arr[n];
    for (int i = 0; i < n; i++) {
        arr[i].first = a[i];
        arr[i].second = i;
    }
  
    // Sort the array elements
    sort(arr, arr + n);
  
    // Answer all the queries
    for (int i = 0; i < m; i++)
        cout << answerQuery(arr, q[i][0], q[i][1]) << endl;
}
  
// Driver code
int main()
{
    int a[] = { 1, 5, 4, 2, 8, 6, 7 };
    int n = sizeof(a) / sizeof(a[0]);
    int q[][2] = { { 2, 5 }, { 1, 3 }, { 1, 5 }, { 3, 6 } };
    int m = sizeof(q) / sizeof(q[0]);
    solveQueries(a, n, q, m);
  
    return 0;
}


Java
// Java implementation of the approach 
import java.util.*;
import java.util.Comparator;
  
class GFG
{
      
// pair classs
static class pair
{
    int first,second;
    pair(int f, int s)
    {
        first = f;
        second = s;
    }
}
  
// Fucntion to return the result 
// for a particular query 
static int answerQuery(pair arr[], int l, int r) 
{ 
    // Get the difference between the indices of 
    // L-th and the R-th smallest element 
    int answer = Math.abs(arr[l - 1].second -
                        arr[r - 1].second); 
  
    // Return the answer 
    return answer; 
} 
  
// Function that performs all the queries 
static void solveQueries(int a[], int n, 
                        int q[][], int m) 
{ 
  
    // Store the array numbers 
    // and their indices 
    pair arr[] = new pair[n]; 
    for (int i = 0; i < n; i++) 
    {
        arr[i] = new pair(0, 0);
        arr[i].first = a[i]; 
        arr[i].second = i; 
    } 
        // Sort pair
        Comparator comp = new Comparator() 
        {
              
            public int compare(pair e1, pair e2) 
            {
                if(e1.first < e2.first)
                {
                    return -1;
                }
                else if (e1.first > e2.first)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
        };
          
    // Sort the array elements 
    Arrays.sort(arr,comp); 
  
    // Answer all the queries 
    for (int i = 0; i < m; i++) 
        System.out.println( answerQuery(arr, q[i][0], q[i][1])); 
} 
  
// Driver code 
public static void main(String args[])
{ 
    int a[] = { 1, 5, 4, 2, 8, 6, 7 }; 
    int n = a.length; 
    int q[][] = { { 2, 5 }, { 1, 3 }, { 1, 5 }, { 3, 6 } }; 
    int m = q.length; 
    solveQueries(a, n, q, m); 
} 
}
  
// This code is contributed by Arnab Kundu


Python3
# Python 3 implementation of the approach
  
# Fucntion to return the result
# for a particular query
def answerQuery(arr, l, r):
      
    # Get the difference between the indices 
    # of L-th and the R-th smallest element
    answer = abs(arr[l - 1][1] - 
                 arr[r - 1][1])
  
    # Return the answer
    return answer
  
# Function that performs all the queries
def solveQueries(a, n, q, m):
      
    # Store the array numbers
    # and their indices
    arr = [[0 for i in range(n)] 
              for j in range(n)]
    for i in range(n):
        arr[i][0] = a[i]
        arr[i][1] = i
  
    # Sort the array elements
    arr.sort(reverse = False)
  
    # Answer all the queries
    for i in range(m):
        print(answerQuery(arr, q[i][0], 
                               q[i][1]))
  
# Driver code
if __name__ == '__main__':
    a = [1, 5, 4, 2, 8, 6, 7]
    n = len(a)
    q = [[2, 5], [1, 3], 
         [1, 5], [3, 6]]
    m = len(q)
    solveQueries(a, n, q, m)
  
# This code is contributed by
# Surendra_Gangwar


输出:
2
2
5
4

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。