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.
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.
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)
Show that the set of complex numbers ℂ= {x+iy|x and y are real, i2=-1} is a...
Show that the set of complex numbers ℂ= {x+iy|x and y are real, i2=-1} is a vector space. Verify each of the 10 axioms.
Suppose that the array X consists of real numbers X[1], X[2], …, X[N]. Write a pseudocode...
Suppose that the array X consists of real numbers X[1], X[2], …, X[N]. Write a pseudocode program to compute the minimum of these numbers.
Let S be the set of all ordered pairs of real numbers. Define scalar multiplication and...
Let S be the set of all ordered pairs of real numbers. Define scalar multiplication and addition on S by: α(x1,x2)=(αx1,αx2) (x1,x2)⊕(y1,y2)=(x1 +y1,0) We use the symbol⊕to denote the addition operation for this system in order to avoid confusion with the usual addition x+y of row vectors. Show that S, together with the ordinary scalar multiplication and the addition operation⊕, is not a vector space. Test ALL of the eight axioms and report which axioms fail to hold.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT