Queries to return the absolute difference between L-th smallest number and the R-th smallest number in C++ Program

C++Server Side ProgrammingProgramming

In this problem, we are given an array arr[] of size n and Q queries each consisting of 2 values L and R. Our task is to create a program to solve queries to return the absolute difference between L-th smallest number and the R-th smallest number.

Problem Description − To solve each query, we need to find the index of Lth smallest and Rth smallest number. And find the difference between these indices.

Let’s take an example to understand the problem,

Input

arr[] = {8, 4, 1, 5, 2} Q = 2 Queries[][] = {{2, 4}, {1, 5}}

Output

1 2

Explanation

For {2, 4}: 2nd smallest element is 2 whose index is 4
4th smallest element is 5 whose index is 3 Difference = 4 - 3 = 1
For {1, 5} Smallest element is 1 whose index is 2
5th smallest element is 8 whose index is 0 Difference = 2 - 0 = 2

Solution Approach

To solve the problem, we will create a pair that will store the indexes and values of the array elements. And find the i-th smallest element for both L and R values of each query. Then print the absolute difference of their indices.

Program to illustrate the working of our solution,

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void solveAllQueries(int arr[], int n,int Q, int queries[][2] ) {
   pair<int, int> arrayIndex[n];
   for (int i = 0; i < n; i++) {
      arrayIndex[i].first = arr[i];
      arrayIndex[i].second = i;
   }  
   sort(arrayIndex, arrayIndex + n);
   for (int i = 0; i < Q; i++){
      int result = ( abs(arrayIndex[queries[i][0] - 1].second - arrayIndex[queries[i][1] - 1].second) );
      cout<<"For Query "<<(i+1)<<": Difference is "<<result<<endl;
   }
}
int main() {
   int arr[] = { 8, 4, 1, 5, 2 };
   int n = sizeof(arr) / sizeof(arr[0]);
   int Q = 2; int queries[][2] = { { 2, 4 }, { 1, 5 }};
   solveAllQueries(arr, n, Q, queries);
   return 0;
}

Output

For Query 1: Difference is 1
For Query 2: Difference is 2

This program uses pair data structure but there is a solution that can find the difference without using it. For this, we will use an array that will store the elements in ascending order. And then finding ith smallest element for both values of L and R for each query. Then find the difference of their indices.

Program to illustrate the working of our solution,

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int searchEle(int arr[], int ele, int n){
    for(int i = 0; i < n; i++)
   if(arr[i] == ele)
      return i;
      return -1;
}
int findDifference(int arr[], int minArray[], int n, int L, int R){
   int Lele = minArray[L-1];
   int Rele = minArray[R-1];
   int index1 = searchEle(arr, Lele, n);
   int index2 = searchEle(arr, Rele, n);
   return abs(index1 - index2);
 }
void solveAllQueries(int arr[], int n,int Q, int queries[][2] ) {
   int minArray[n];
   for (int i = 0; i < n; i++)
   minArray[i] = arr[i];
   sort(minArray, minArray + n);
   for(int i = 0; i < Q; i++){
      cout<<"For Query "<<(i+1)<<": Difference is "<<findDifference(arr, minArray, n,       queries[i][0], queries[i][1])<<endl;
   }
}
int main() {
   int arr[] = { 8, 4, 1, 5, 2 };
   int n = sizeof(arr) / sizeof(arr[0]);
   int Q = 2; int queries[][2] = { { 2, 4 }, { 1, 5 }};
   solveAllQueries(arr, n, Q, queries); return 0;
}

Output

For Query 1: Difference is 1
For Query 2: Difference is 2
raja
Published on 22-Dec-2020 08:43:26
Advertisements