Counting elements in two arrays (Function Program)

Counting elements in two arrays (Function Program)

Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[].

Input:

The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains two integers m and n denoting the size of the two arrays. The following two lines will contain both the arrays respectively.

Output:

Print the count of elements less than or equal to it in arr2 for each element in arr1.

Constraints:

1<=T<=10^5
1<=m,n<=10^5
1<=arr1[i],arr2[j]<=10^5

Example:

Input:

2
6 6
1 2 3 4 7 9
0 1 2 1 1 4
5 7
4 8 7 5 1
4 48 3 0 1 1 5

Solution:

   So when i read this question at that time i get some basic idea that :
     1->What is the approach for solving this Question?
     2-> Can i optimize its solution ?
so whenever you get new question first asked all question from yourself that which algorithm and which data structure i am going to used and by using this algorithm , Am i pass all the test-case?
So when you try this type of strategy than its help you revise your concept and also help you to think about different type of solution.

So finally what i do in this question that......
If we read this question than we get idea that by traversing each time in second array we got our answer . But its take more time to traverse each time entire array so what i do that i sort the second array and only traverse till that less than number i get.
for example:
    when this condition (arr[i]>=array2[j]) is true i count otherwise i stop the loop and print the count.

Post a Comment

0 Comments