Question

In: Computer Science

You are given a set S of n real numbers where for each element x ∈...

You are given a set S of n real numbers where for each element x ∈ S, 1 < | x | < 10. You are also given a black box that returns true if there is some combination (x, y, z) ∈ S such that x · y = z.
Say the black box returns only z. Write an O(n log n) algorithm to find x and y. In other words, given a set S with n real numbers and a number z, write an O(n log n) algorithm to find (x, y) ∈ S such that x · y = z.

Solutions

Expert Solution

O(nlogn) algorithm to solve this problem:

1) Sort the set S if, they are not sorted

2) loop over the set and for each element a in the set:

    1) Find b = z/a

    2) Then, search for b in the set using Binary Search.

    3) If found return true

4) return false

Time complexity analysis of algorithm

Lets say set has n elements,

sorting the array ------ O(nlogn)          [use quick sort or merge sort or heap sort]

looping over the set   ------ O(n). And for each element a in the set, we are finding a number b such that a*b = z. For this we will use Binay Search

Binary search ------- O(logn)

So total time complexity of looping over the set and searching for a number b such that a*b = z is O(n)*O(logn) = O(nlogn)

Total time complexity of algorithm = O(nlogn)sorting + O(nlogn)looping + searching = O(nlogn)


Related Solutions

Let S be a set of n numbers. Let X be the set of all subsets...
Let S be a set of n numbers. Let X be the set of all subsets of S of size k, and let Y be the set of all ordered k-tuples (s1, s2,   , sk) such that s1 < s2 <    < sk. That is, X = {{s1, s2,   , sk} | si  S and all si's are distinct}, and Y = {(s1, s2,   , sk) | si  S and s1 < s2 <    < sk}. (a) Define a one-to-one correspondence f : X → Y. Explain...
Please prove that: A nonempty compact set S of real numbers has a largest element (called...
Please prove that: A nonempty compact set S of real numbers has a largest element (called the maximum) and a smallest element (called the minimum). By the way, I think a minimum is provided by -max(-S)
The question is correct. Let X be an n-element set of positive integers each of whose...
The question is correct. Let X be an n-element set of positive integers each of whose elements is at most (2n - 2)/n. Use the pigeonhole principle to show that X has 2 distinct nonempty subsets A ≠ B with the property that the sum of the elements in A is equal to the sum of the elements in B.
Given an array A[0 … n-1], where each element of the array represent a vote in...
Given an array A[0 … n-1], where each element of the array represent a vote in the election. Assume that each vote is given as an integer representing the ID of the chosen candidate. Can you determine who wins the election? What is the complexity of your solution? Hint: it is similar to finding the element that is repeated the maximum number of times.
Show that |N| = |Z|, where N is the set of natural numbers and Z is...
Show that |N| = |Z|, where N is the set of natural numbers and Z is the set of all integers.
Consider the set of real numbers S=[-1,0)U{1/n:n=1,2,3,...}. Give the explicit set for each of the The...
Consider the set of real numbers S=[-1,0)U{1/n:n=1,2,3,...}. Give the explicit set for each of the The complement of S, The closure of S, The boundary points of S, The limit points of S, The isolated points of S, The interior of S, and The exterior of S.
Problem 1: Given an array A[0 ... n-1], where each element of the array represents a...
Problem 1: Given an array A[0 ... n-1], where each element of the array represents a vote in the election. Assume that each vote is given as integers representing the ID of the chosen candidate. Write the code determining who wins the election. Problem 2: How do we find the number which appeared maximum number of times in an array? ( Use Java and an original code )
Using Java, Given an array A[0 ... n-1], where each element of the array represent a...
Using Java, Given an array A[0 ... n-1], where each element of the array represent a vote in the election. Assume that each vote is given as an integer representing the ID of the chosen candidate. Can you determine who wins the election? What is the complexity of your solution? Hint: it is similar to finding the element that is repeated the maximum number of times.
Given an array A of n distinct real numbers, we say that a pair of numbers...
Given an array A of n distinct real numbers, we say that a pair of numbers i, j ∈ {0, . . . , n−1} form an inversion of A if i < j and A[i] > A[j]. Let inv(A) = {(i, j) | i < j and A[i] > A[j]}. Answer the following: (a) How small can the number of inversions be? Give an example of an array of length n with the smallest possible number of inversions. (b)...
Given that α (alpha) is an upper bound of a given set of S of real...
Given that α (alpha) is an upper bound of a given set of S of real numbers, prove the following are equivalent: α = sup(S) α ∈ cl(A)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT