Question

In: Computer Science

Let A[1 · · · n] be an array of n elements and B[1 · ·...

Let A[1 · · · n] be an array of n elements and B[1 · · · m] an array of m elements. We assume that mn. Note that neither A nor B is sorted. The problem is to compute the number of elements of A that are smaller than B[i] for each element B[i] with 1 ≤ im.

For example, let A be {30, 20, 100, 60, 90, 10, 40, 50, 80, 70} of ten elements. Let B be {60, 35, 73} of three elements. Then, your answer should be the following: for 60, return 5 (because there are 5 numbers in A smaller than 60); for 35, return 3; for 73, return 7.

(a) Design an O(mn) time algorithm for the problem. (10 points)

(b) Improve your algorithm to O(n log m) time. (20 points)
Hint: Use the divide and conquer technique. Since mn, you cannot sort the array A because that would take O(n log n) time, which is not O(n log m) as m may be much smaller than n.

Note: For this problem, you need to describe the main idea of your algorithms and briefly analyze their time complexities. You will receive the full 30 points if you give an O(n log m) time algorithm directly for (b) without giving any algorithm for (a).

Solutions

Expert Solution

#include<bits/stdc++.h>
using namespace std;

int main(){

    vector<int> A = {30, 20, 100, 60, 90, 10, 40, 50, 80, 70} ;
    vector<int> B = {60,35,73};

    sort(B.begin(),B.end());  // sort the B vector   time complexity O(m(logm))
    const int m = B.size(); 
    const int n = A.size(); 
    int fre[m+1];       // this will store the frequency of elements in A smaller than a elements in B
    memset(fre,0,sizeof(fre));


    /*
     * Algo :
     * for every element of A 
     *      find the element in B which is strictly less than that element
     *      and increase  the frequency of that index by one.
     *
     *
     *  At last take the cummlative sum of the frequency array to find the required ans
     */



    for(int i=0;i<n; i++){   // this is binary search in O(logm) time for one A's element 
        int low = 0;
        int high = m-1;
        int ans = -1;
        while(low <= high){
            int mid = low + (high - low)/2;
            if(B[mid] < A[i]){
                ans = mid;
                low = mid + 1;
            }
            else{
                high =mid  - 1;
            }
        }
        if(ans == -1){
            fre[0]++;
        }
        else{
            fre[ans+1]++;
        }
    }
    for(int i=1;i<=m;i++) fre[i] += fre[i-1];
    for(int i=0;i<m;i++){
        cout<<"No of values samller than "<<B[i]<<" are: "<<fre[i]<<endl;
    }
}

Related Solutions

Let A[1 · · · n] be an array of n elements and B[1 · ·...
Let A[1 · · · n] be an array of n elements and B[1 · · · m] an array of m elements. We assume that m ≤ n. Note that neither A nor B is sorted. The problem is to compute the number of elements of A that are smaller than B[i] for each element B[i] with 1 ≤ i ≤ m. For example, let A be {30, 20, 100, 60, 90, 10, 40, 50, 80, 70} of ten...
Let A be a set with m elements and B a set of n elements, where...
Let A be a set with m elements and B a set of n elements, where m; n are positive integers. Find the number of one-to-one functions from A to B.
Let G be a group of order mn where gcd(m,n)=1 Let a and b be elements...
Let G be a group of order mn where gcd(m,n)=1 Let a and b be elements in G such that o(a)=m and 0(b)=n Prove that G is cyclic if and only if ab=ba
Let G be a group,a;b are elements of G and m;n are elements of Z. Prove...
Let G be a group,a;b are elements of G and m;n are elements of Z. Prove (a). (a^m)(a^n)=a^(m+n) (b). (a^m)^n=a^(mn)
Let iqsort(A, 1, n) be an algorithm that sorts an array A with n integers. It...
Let iqsort(A, 1, n) be an algorithm that sorts an array A with n integers. It works as follows: iqsort(A, p, q){ if p ≥ q, return; r=partition(A, p, q); //run quick sort on the low part quicksort(A, p, r − 1); //run insert sort on the high part insertsort(A, r + 1, q); } Compute the best-case, worst-case, and average-case complexities of iqsort.
Let A[1..n] be an array of distinct positive integers, and let t be a positive integer....
Let A[1..n] be an array of distinct positive integers, and let t be a positive integer. (a) Assuming that A is sorted, show that in O(n) time it can be decided if A contains two distinct elements x and y such that x + y = t. (b) Use part (a) to show that the following problem, re- ferred to as the 3-Sum problem, can be solved in O(n2) time: 3-Sum Given an array A[1..n] of distinct positive integers, and...
Let A[1..n] be an array of distinct positive integers, and let t be a positive integer....
Let A[1..n] be an array of distinct positive integers, and let t be a positive integer. (a) Assuming that A is sorted, show that in O(n) time it can be decided if A contains two distinct elements x and y such that x + y = t. (b) Use part (a) to show that the following problem, re- ferred to as the 3-Sum problem, can be solved in O(n2) time: 3-Sum Given an array A[1..n] of distinct positive integers, and...
Write a small C program connect.c that: 1. Initializes an array id of N elements with...
Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and changing all the entries with the same name as p to have the same...
Write a small C program connect.c that: 1. Initializes an array id of N elements with...
Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and changing all the entries with the same name as p to have the same...
(2) Let Z/nZ be the set of n elements {0, 1, 2, . . . ,...
(2) Let Z/nZ be the set of n elements {0, 1, 2, . . . , n ? 1} with addition and multiplication modulo n. (a) Which element of Z/5Z is the additive identity? Which element is the multiplicative identity? For each nonzero element of Z/5Z, write out its multiplicative inverse. (b) Prove that Z/nZ is a field if and only if n is a prime number. [Hint: first work out why it’s not a field when n isn’t prime....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT