Problem of the day
Malay is the food inspector of the Surat district. There are two main shops in the district of Surat namely shop ‘A’ and shop ‘B’, having ‘N’ and ‘M’ number of food products denoted by array ‘ARR1’ and ‘ARR2’ respectively. He has been assigned the task to find the number of edible products with the same name but have different prices in those two main shops of the district based on which he can decide the inflation rate of food products in Surat.
But because there is another big food industry inspection, he doesn’t have time to manually check the prices of every product in both shops, so being his friend he asked you to help him find the count of products with the same name but different prices in both shops.
EXAMPLE :Input: ‘N’ = 2, ‘M' = 2, ‘ARR1’ = [{“bread”, 10}, {“rice”, 20}], ‘ARR2’ = [{“milk”, 25}, {“rice”, 20}]
Output: 0
In this case, here the only same product in both shops is “rice” and the price is the same in both shops (i.e. 20). Thus there is no product with the same name but at different prices.
The first line will contain the integer ‘T’, the number of test cases.
The first line of each test case contains two integers, ‘N’ and ‘M’ separated by spaces.
Followed by ‘N’ lines, each containing the name of the product ‘foodItem’ and the non-negative integer price of the products ‘P’, denoting the food products of the shop ‘A’.
Followed by ‘M’ lines, each containing the name of the product ‘foodItem’ and the non-negative integer price of the products ‘P’, denoting the food products of the shop ‘B’.
Output format :
For each test case, print the number of food products having the same name but having different prices in shop ‘A’ and shop ‘B’.
Note :
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 10
1 <= ‘N’ <= 10^4
1 <= ‘M’ <= 10^4
It is guaranteed that ‘foodItem’ consists of only lower-case English letters.
It is guaranteed that the length of ‘foodItem’ is less than ‘10’.
It is guaranteed that the name (i.e. ‘foodItem’) of every product in a single shop is distinct.
0 <= ‘P’ <= 10^9
It is guaranteed that sum of the lengths of ‘foodItem’ is <= 10^5.
It is guaranteed that sum of ‘N’ over all test cases is <= 10^4
It is guaranteed that sum of ‘M’ over all test cases is <= 10^4
Time Limit: 1 sec
2
3 2
wheat 10
potato 20
tomato 17
wheat 15
tomato 17
1 1
onion 1
onion 80000
1
1
For the first test case, “wheat” has a price of ‘10’ in shop ‘A’ and ‘15’ in shop ‘B’, other products have the same price.
Hence, the output will be: 1
For the second test case, “onion” has a price of ‘1’ in shop ‘A’ and ‘80000’ in shop ‘B’.
Hence, the output will be: 1
2
5 3
abcd 3
zyxw 7
corn 13
coffee 49
tea 69
dcba 8
wxyz 4
chilli 10
2 2
milk 30
chocolate 5
milk 30
chocolate 5
0
0
Can we try to match the prices of products with the same name?
The idea for this approach is to iterate on the products of any shop and try to find a product with the same name in another shop and match their prices.
So we can keep a variable ‘count’, denoting the number of unmatched products price, initially set to 0. Now we iterate on the products of shop ‘A’ (i.e. iterate on the array ‘ARR1’) and for each ‘i’ ∈ ‘[0, N-1]’ we try to find the product in shop ‘B’ (i.e. ‘ARR2’) that has the same name as ‘ith’ product of shop ‘A’ but have different price. If we find such a product in shop ‘B’ we increment the ‘count’ by ‘1’ otherwise if there exists a product with the same name and the same price or product with that name doesn’t exist we move to the next product.
Algorithm:
// The function will return the count of products with the same name and different prices.
Int differentPrices(N, M, ARR1, ARR2)
O( N * M * L ), Where ‘N’ and ‘M’ are input integers and ‘L’ denotes the longest name of any food item.
In the above algorithm, in the worst case when no products of either shop are the same, for every product of shop ‘A’ we will iterate through every product of shop ‘B’ and we will compare both the product names, which in the worst case can take ‘L’ comparisons when both names are equal, in total ‘N*M*L’ iterations.
Hence the time complexity will be O( N * M * L )
O( 1 ),
In the above algorithm, we use the variable ‘COUNT’ and some temporary loop variables only.
Hence the space complexity will be O( 1 ).