Q 3. (5 marks) Let A be an array contains n pair of integer values. Let two pairs (e, f) and (g, h) are symmetric if g is equals to f and h is equals to e. Write pseudocode for an algorithm that decides whether a given pair of input array A of size n is symmetric. If pair in A is symmetric the algorithm must return the value true, otherwise it must return the value false. 0 1 30 10 0 1 5 15 0 1 10 30 0 1 18 33 0 1 15 30 0 1 33 18 Algorithm must return false if a single pair does not hold symmetric relation with any other pair in an array A. Suppose [x, y] is non-symmetric then the algorithm will return -1 or false. (a) (1 mark) Explain what the worst case for the algorithm is. (b) (4 marks) Compute the time complexity of the algorithm in the best case. You must give the order of the time complexity using "big-Oh" notation and you must explain how you computed the time complexity.
Added by Albert M.
Close
Step 1
- We need to determine if there exists a symmetric pair in an array of integer pairs. Two pairs (e, f) and (g, h) are symmetric if g = f and h = e. Show more…
Show all steps
Your feedback will help us improve your experience
Akash M and 80 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
Akash M.
You are given an array A of n elements whose entries are distinct integers. Assume that n ≥ 2. An index j is called a local minimum of A if one of the following conditions holds: • j = 1 and A[1] < A[2]. • j = n and A[n] < A[n − 1]. • 1 < j < n, A[j] < A[j − 1], and A[j] < A[j + 1]. Find a local minimum of A in O(log n) time. Go through each step, be very systematic. a. Convince an understanding of what a local minimum is by giving an example of an array A[1 . . . 10] of distinct integers such that none of the indices in {1, 2, 3, 4, 5} is a local minimum, but A has three different local minima. b. From now on, consider a general n. Prove that A is guaranteed to have a local minimum. Keep it brief. c. Provide pseudocode for a recursive divide-and-conquer algorithm that solves the problem of finding a local minimum of A. Prove the correctness of your algorithm by appropriately referring to or extending your previous proof. d. Prove that your algorithm’s worst-case runtime T(n) satisfies T(n) ≤ T(n/2) + O(1). Solve the recurrence, in an asymptotic sense, to complete the algorithm analysis.
Sri K.
Problem 2 (Closest element) You are given as an input two arrays of n distinct natural numbers A = [a1, a2, ..., an] and B = [b1, b2, ..., bn]. For each element ai of A, you want to output the element bj of B that is closest to ai. More precisely, you want to find bj such that min | ai - bk | = |ai - bj | where 1 ≤ k ≤ n. You will output a new array C storing bj at position i. For example, for A = [6, 1, 12] and B = [1, 3, 10], we have C = [3, 1, 10] since the closest element of B to a1 = 6 is b2 = 3, the closest to a2 = 1 is b1 = 1, and the closest to a3 = 12 is b3 = 10. Note that C could have repeated entries, for example for A = [6, 1, 12] and B = [1, 0, 8] we have C = [8, 1, 8]. Design an algorithm to find C. Describe your algorithm with words and state and analyze its running time.
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
Transcript
Watch the video solution with this free unlock.
EMAIL
PASSWORD