给定一个由N个唯一元素和Q个查询组成的数组arr [] 。每个查询由两个整数L和R组成。的任务是打印为L的第最小索引和第r个最小的元素之间的绝对差。
例子:
Input: arr[] = {1, 5, 4, 2, 8, 6, 7},
que[] = {{2, 5}, {1, 3}, {1, 5}, {3, 6}}
Output:
2
2
5
4
For the first query the second smallest number is 2, which is at index 3
and the 5th smallest number is 6 which is at index 5. Hence the
absolute difference between both such index is 2.
Input: arr[] = {1, 2, 3, 4},
que[] = {{1, 1}, {2, 3}}
Output:
0
1
方法:可以按照以下步骤解决上述问题。
- 将元素及其索引存储在成对的数组中。
- 根据第一个元素对数组进行排序。
- 对于每个查询,答案都是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