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 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 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 m ≤ n, 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

Solution

Part a

  • Pick one element from array B and for the selected element of array B scan the elements of array A one by one to find which element is larger and which is smaller.
  • Print the element and the corresponding count of elements in A which are smaller than this element of array B.
  • Follow the above steps for all the m elements of array B.

Complexity analysis

Since for each element of array B complete array A is scanned.

Number of elements in array A is n

Number of elements in array B is m

So the complexity of the above algorithm is O(mn)

Part b

Sort the elements of array B. Sorting of elements in array B takes O(mlogm) time complexity.

Create a new array C of size m which stores the count of elements of array A which are smaller than element B[i].

i.e C[0] stores count of elements smaller than B[0]

C[1] stores count of elements smaller than B[1]

C[2] stores count of elements smaller than B[2] and so in.

Initialize all the elements of array C to zero.

Scan the elements of array A one by one and follow the below mentioned steps:

  • for each element x of A, perform the binary search in array B.
  • Binary search is used to identify the elements in array B larger than x.
  • Increment the count of all the values at the index which we identify using the binary search.

At the end after the array A is scanned completely at index i in array C , the count of all the elements that are smaller than B[i] is stored.

Since binary seach takes O(logm)

And binary search is performed n times.

So total time complexity = Time to sort array B + Time to identify smaller elements using binary search

= O(mlogm) + O(nlogm)

= O(nlogm) (since n>m)


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