28. Counting Elements in Two Arrays
โ GFG solution to count elements in array b that are less than or equal to each element in array a using frequency array and prefix sum technique. ๐
๐งฉ Problem Description
๐ Examples
Example 1
Input: a[] = [4, 8, 7, 5, 1], b[] = [4, 48, 3, 0, 1, 1, 5]
Output: [5, 6, 6, 6, 3]
Explanation:
For a[0] = 4, there are 5 elements in b (4, 3, 0, 1, 1) that are โค 4.
For a[1] = 8 and a[2] = 7, there are 6 elements in b that are โค 8 and โค 7.
For a[3] = 5, there are 6 elements in b that are โค 5.
For a[4] = 1, there are 3 elements in b (0, 1, 1) that are โค 1.Example 2
Input: a[] = [10, 20], b[] = [30, 40, 50]
Output: [0, 0]
Explanation:
For a[0] = 10 and a[1] = 20, there are no elements in b that are less than or equal to 10 or 20. Hence, the output is [0, 0].๐ Constraints
โ
My Approach
Frequency Array + Prefix Sum
๐ Time and Auxiliary Space Complexity
๐งโ๐ป Code (C++)
๐งโ๐ป Code (Java)
๐ Code (Python)
๐ง Contribution and Support
๐Visitor Count
Last updated