Number of comparisons in each direction for m queries in linear search
Given an array containing N distinct elements. There are M queries, each containing an integer X and asking for the index of X in the array. For each query, the task is to perform linear search X from left to right and count the number of comparisons it took to find X and do the same thing right to left. In the end, print the total number of comparisons in both directions among all the queries.
Examples:
Input: arr[] = {1, 2}, q[] = {1, 2}
Output: 3, 3
For 1-based indexing
For 1st query : Number of comparisons from left to right is 1 and from right to left is 2
For 2nd query : Number of comparisons from left to right is 2 and from right to left is 1
Input: arr[] = {-1, 2, 4, 5, 1}, q[] = {-1, 4, 2}
Output: 3, 7
Approach: Find the index at which X is present in the array say i (1-based indexing), the number of comparisons for left to right would be i and the number of comparisons for right to left would be (n – i + 1). All we need to do is to find the index quickly. It can be done by using a map in which key is the element’s value and value is the index.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
pair< int , int > countCamparisions( int n, int arr[], int m, int qry[])
{
int i;
unordered_map< int , int > index;
for (i = 1; i <= n; i++) {
index[arr[i]] = i;
}
int ltr = 0, rtl = 0;
for (i = 1; i <= m; i++) {
int x = qry[i];
ltr += index[x];
rtl += n - index[x] + 1;
}
return make_pair(ltr, rtl);
}
int main()
{
int arr[] = { -1, 2, 4, 5, 1 };
int n = ( sizeof (arr) / sizeof (arr[0])) - 1;
int q[] = { -1, 4, 2 };
int m = ( sizeof (q) / sizeof (q[0])) - 1;
pair< int , int > res = countCamparisions(n, arr, m, q);
cout << res.first << " " << res.second;
}
|
Java
import java.util.HashMap;
import java.util.Map;
class GFG
{
static Pair<Integer, Integer> countCamparisions( int n,
int arr[], int m, int qry[])
{
int i;
HashMap<Integer,Integer> index = new HashMap<>();
for (i = 1 ; i <= n; i++)
{
index.put(arr[i], i);
}
int ltr = 0 , rtl = 0 ;
for (i = 1 ; i <= m; i++)
{
int x = qry[i];
ltr += index.get(x);
rtl += n - index.get(x) + 1 ;
}
Pair<Integer, Integer> ans = new Pair<>(ltr, rtl);
return ans;
}
public static void main(String []args)
{
int arr[] = { - 1 , 2 , 4 , 5 , 1 };
int n = arr.length - 1 ;
int q[] = { - 1 , 4 , 2 };
int m = q.length - 1 ;
Pair<Integer, Integer> res = countCamparisions(n, arr, m, q);
System.out.println(res.first + " " + res.second);
}
}
class Pair<A, B>
{
A first;
B second;
public Pair(A first, B second)
{
this .first = first;
this .second = second;
}
}
|
Python3
def countCamparisions(n, arr, m, qry) :
index = {}
for i in range ( 1 , n + 1 ) :
index[arr[i]] = i
ltr, rtl = 0 , 0
for i in range ( 1 , m + 1 ) :
x = qry[i]
ltr + = index[x]
rtl + = n - index[x] + 1
return (ltr, rtl)
if __name__ = = "__main__" :
arr = [ - 1 , 2 , 4 , 5 , 1 ]
n = len (arr) - 1
q = [ - 1 , 4 , 2 ]
m = len (q) - 1
res = countCamparisions(n, arr, m, q)
print (res[ 0 ], res[ 1 ])
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static Pair< int ,
int > countCamparisions( int n, int []arr,
int m, int []qry)
{
int i;
Dictionary< int ,
int > index = new Dictionary< int ,
int >();
for (i = 1; i <= n; i++)
{
index.Add(arr[i], i);
}
int ltr = 0, rtl = 0;
for (i = 1; i <= m; i++)
{
int x = qry[i];
ltr += index[x];
rtl += n - index[x] + 1;
}
Pair< int ,
int > ans = new Pair< int ,
int >(ltr, rtl);
return ans;
}
public static void Main(String []args)
{
int []arr = { -1, 2, 4, 5, 1 };
int n = arr.Length - 1;
int []q = { -1, 4, 2 };
int m = q.Length - 1;
Pair< int , int > res = countCamparisions(n, arr, m, q);
Console.WriteLine(res.first + " " + res.second);
}
}
class Pair<A, B>
{
public A first;
public B second;
public Pair(A first, B second)
{
this .first = first;
this .second = second;
}
}
|
Javascript
<script>
function countCamparisions(n, arr, m, qry)
{
var i;
var index = new Map();
for (i = 1; i <= n; i++) {
index.set(arr[i], i);
}
var ltr = 0, rtl = 0;
for (i = 1; i <= m; i++) {
var x = qry[i];
ltr += index.get(x);
rtl += n - index.get(x) + 1;
}
return [ltr, rtl];
}
var arr = [-1, 2, 4, 5, 1];
var n = arr.length - 1;
var q = [-1, 4, 2];
var m = q.length - 1;
var res = countCamparisions(n, arr, m, q);
document.write( res[0] + " " + res[1]);
</script>
|
Complexity Analysis:
- Time Complexity: O(N + M)
- Auxiliary Space: O(N)
Last Updated :
09 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...