- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Queries to return the absolute difference between L-th smallest number and the R-th smallest number in C++ Program
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
#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
#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
- Related Articles
- Queries to return the absolute difference between Lth smallest number and the R-th smallest number in C++
- Find the number with set bits only between L-th and R-th index using C++
- Queries to answer the X-th smallest sub-string lexicographically in C++
- Find the k-th smallest divisor of a natural number N in C++
- Program to find out the k-th smallest difference between all element pairs in an array in C++
- K-th Smallest Prime Fraction in C++
- K-th Smallest in Lexicographical Order in C++
- Find K-th Smallest Pair Distance in C++
- What is the smallest natural number and smallest whole number?
- C/C++ Program for the n-th Fibonacci number?
- Find the difference between the largest 3 digit number and the smallest 6 digit number.
- Find the difference between the largest 8-digit number and the smallest 6-digit number.
- 8085 Program to find the smallest number
- Find the difference between the smallest 3 digit prime number and largest 1digit prime number
- Find the difference between the smallest number of 7 digits and the largest number of digits.
