Question

In: Computer Science

Consider an array A[1 · · · n] which is sorted and then rotated k steps...

Consider an array A[1 · · · n] which is sorted and then rotated k steps to the right. For example, we might start with the sorted array [1, 4, 5, 9, 10], and rotate it right by k = 3 steps to get [5, 9, 10, 1, 4]. Give an O(log n)-time algorithm that finds and returns the position of a given element x in array A, or returns None if x is not in A. Your algorithm is given the array A[1 · · · n] but does not know k. You can use C, C++, Java, or Python to solve this problem (ask the instructor if you have another programming language in mind). Python3 pls

Solutions

Expert Solution


Related Solutions

Consider an array A[1 · · · n] which is sorted and then rotated k steps...
Consider an array A[1 · · · n] which is sorted and then rotated k steps to the right. For example, we might start with the sorted array [1, 4, 5, 9, 10], and rotate it right by k = 3 steps to get [5, 9, 10, 1, 4]. Give an O(log n)-time algorithm that finds and returns the position of a given element x in array A, or returns None if x is not in A. Your algorithm is...
(a) Consider the general k-ary search algorithm (generalization of binary search) which splits a sorted array...
(a) Consider the general k-ary search algorithm (generalization of binary search) which splits a sorted array of size n into k subsets each of size n/k and recursively searches only one of these k subsets. Which one of these k subsets should be searched is decided by making (k − 1) comparisons, each of which can be done in constant time. Clearly, the recurrence relation for this k-ary search algorithm can be written as, T(n) = T(n/k) + (k −...
Consider the following checkSum() method where N numbers are saved in an array (as sorted in...
Consider the following checkSum() method where N numbers are saved in an array (as sorted in as- cending order). In the main() function, the user will input a number. The checkSum() method will find out whether the sum of any two numbers in the array is equal to the given number by the user. You need to solve the problem with minimum time complexity (low complexity will lead to higher marks!).
Given an array A[1..n], with distinct values and k with 1 ≤ k ≤ n. We...
Given an array A[1..n], with distinct values and k with 1 ≤ k ≤ n. We want to return the k smallest element of A[1.....n], in non-decreasing order. For example: A = [5, 4, 6, 2, 10] and k = 4, the algorithm returns [2, 4, 5, 6]. There are at least the following four approaches: a. heapify A and then extract k elements one by one b. sort the array (e.g. using MergeSort or HeapSort) and then read the...
Denition: An orthogonal array OA(k, n) on n symbols is an n2 x k array such...
Denition: An orthogonal array OA(k, n) on n symbols is an n2 x k array such that, in any two columns, each ordered pair of symbols occurs exactly once. Prove that there exists an OA(k, n) if and only if there exist (k - 2) mutually orthogonal Latin squares of order n. (combinatorics and design)
The following is the pseudocode of algorithm that searches a sorted array of n items by...
The following is the pseudocode of algorithm that searches a sorted array of n items by dividing it into three sub arrays of almost n/3 items (7pt). Hint: refer Mergesort algorithm) Input: Sorted array of Keys(A) indexed from 1 to n. Output: Location, the Location of K in array A (hint: return 0 if K is no in A) index location3 (int A[1…n ], index L, index H) { index m1, m2; if (L > H) return 0; else   ...
An abs-sorted array is an array of numbers in which |A[i]| <= |A[j]| whenever i <...
An abs-sorted array is an array of numbers in which |A[i]| <= |A[j]| whenever i < j. For example, the array A = [-49, 75, 103, -147, 164, -197, -238, 314, 348, -422], though not sorted in the standard sense, is abs-sorted. Design an algorithm that takes an abs-sorted array A and a number k, and returns a pair of indices of elements in A that sum up to k. For example if k = 167 your algorithm should output...
Given a positive integer k and an array A[1..n] that contains the quiz scores of n...
Given a positive integer k and an array A[1..n] that contains the quiz scores of n students in ascending order, design a divide and conquer algorithm to efficiently count the number of students that have quiz scores in (100(i − 1)/k, 100i/k] for integers 1 ≤ i ≤ k. Let group i be the set of students with quiz scores in (100(i − 1)/k, 100i/k] for integers 1 ≤ i ≤ k. The counting result should be stored in G[1..k],...
Python - You are given a sorted (from smallest to largest) array A of n distinct...
Python - You are given a sorted (from smallest to largest) array A of n distinct integers which can be positive, negative or zero. Design the fastest algorithm you can for deciding if there is an index i such that A[i] = i.
Let S_k(n) = 1^k + 2^k + ··· + n^k for n, k ≥ 1. Then,...
Let S_k(n) = 1^k + 2^k + ··· + n^k for n, k ≥ 1. Then, S_4(n) is given by S_4(n)= n(n+1)(2n+1)(3n^2 +3n−1)/ 30 Prove by mathematical induction.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT