An array A of n integers is called semi-sorted if it is
increasing until some index and then decreasing afterwards. In
other words, there is some index 1 ≤ p ≤ n such that: • A[i] <
A[i + 1] for 1 ≤ i < p, and • A[i] > A[i + 1] for p ≤ i <
n. Note that in this case, A[p] is the maximum element of A. Give
an algorithm with running time O(logn) that finds the maximum
element of A, assuming that A is semi-sorted. Prove the correctness
of your algorithm as well as do the analysis of the running
time.
Hint: We can use a modified binary search. The idea is if we pick an
index i and compare A[i] with A[i+1] we can determine if i is
before the “peak” (i.e. if i ≤ p) or if i is after the peak: if
A[i] < A[i+1] then we can conclude that the maximum appears in
A[(i + 1),...,n] and if A[i] > A[i + 1] then the maximum is in
A[1,...,i]. For correctness, if your algorithm uses a loop,
formulate a loop invariant (LI) - what remains to be true
throughout all iterations of your loop so that when the loop
terminates, LI implies that the maximum element of A is found? If
your algorithm is defined by recursion, use Induction to prove the
correctness.
Java Program to find the peak element
public class Solution {
public static void main(String[] args) {
//array in which element to be found
int[] A = {4, 5, 6, 7, 0, 1, 2};
// to find the position of minimum index
int pos = findMin(A);
if (pos > 0) {
System.out.println(A[pos - 1]);
} else {
System.out.println(A[A.length - 1]);
}
}
//using binary search to find the peak element
private static int findMin(int[] A) {
int n = A.length;
int low = 0, high = n - 1;
int mid = 0;
//using while loop
while (low <= high) {
mid = low + (high - low) / 2;
//checking if the element is smallest or not
if (mid + 1 < n && mid - 1 >= 0 && A[mid] < A[mid - 1] && A[mid] < A[mid + 1]) {
break;
} else if (mid < n && mid >= 0 && A[mid] > A[high]) {
low = mid + 1;
} else {
high = mid - 1;
}
}
//returning the position of smallest element
return mid;
}
}
Output
7

An array A of n integers is called semi-sorted if it is increasing until some index...
An array A[1,2,... ,n is unimodal if its consists of an increasing sequence followed by sequence a decreasing sequence. More precisely, there exists an index k є {1,2,… ,n} such that there exists an indes . AlE]< Ali1 for all 1 i< k, and Ai]Ali 1 for all k< i< n A1,2,..,n] in O(logn) time the loop invariant (s) that your algorithm maintains and show why they lead to the correctness Give an algorithm to compute the maximum element of...
You are given an array A of integers in sorted order. However, you do not know the length n of the array. Assume that in our programming language arrays are implemented in such a way that you receive an out-of-bounds error message whenever you wish to access an element A[i] with i>n. For simplicity we assume that the error message simply returns the value INT_MAX and that every value in the array is smaller than INT_MAX. (a) Design an algorithm...
6. Let T(1..n] be a sorted array of distinct integers, some of which may be negative. Give an algorithm that can find an index i such that 1 <i<n and T[i] = i, provided such an index exists. Your algorithm should take a time in O(lg n) in the worst case. Answers must be proven (or at least well justified)
Let A = [A[1], A[2],…..,A[n]] be an array of n distinct integers. For 1 <= j <= n, the index j is a happy index if A[i] < A[j] for all 1 <= i < j. Describe an O(n)- time algorithm that finds all the happy indices in the array A. Partial credit will be given for an O(n log(n))-time algorithm and a minimal credit will be given for an O(n^2) –time algorithm. What is the running time of your...
Suppose that we are given a sorted array of distinct integers A[1, ......, n] and we want to decide whether there is an index i for which A[i] = i. Describe an efficient divide-and-conquer algorithm that solves this problem and explain the time complexity. 1. Describe the steps of your algorithm in plain English. 2. Write a recurrence equation for the runtime complexity. 3. Solve the equation by the master theorem.
Suppose you are given an array A holding n distinct integers (negative values are allowed) in sorted order; in other words, A[i] < A[i + 1] for each i ∈ [0, n − 2]. We say the ith element is self referential if A[i] = i. Design an O(log n) time algorithm to determine if there is a self referencial element in the array. Your solution must include a) Statement of your algorithm in plain English. (Pseudo-code is optional.) b)...
1. (16 pts.) Sorted Array Given a sorted array A of n (possibly negative) distinct integers, you want to find out whether there is an index i for which Al = i. Give a divide-and-conquer algorithm that runs in time O(log n). Provide only the main idea and the runtime analysis.
Please answer by mathematical language: An array of n elements is almost sorted if and only if every element is at most k spots away from its actual location. Assuming that you can only perform pairwise comparisons, formally prove that any algorithm which can sort almost sorted arrays must have running time Ω(n log k), You may assume that n is a multiple of k.
3. Write the function find sorted elt whose input is a vector sorted in increasing order and an element x. The output is 0 if the element x does not occur in v, 1 if the element x does occur in v. Below is the algorithm you should implement, known as the binary search algorithm. Note that the code below returns the index, but we want to return true or false, not the index, so adjust your code accordingly. Algorithm....
Suppose we are given two sorted arrays (nondecreasing from index 1 to index n) X[1] · · · X[n] and Y [1] · · · Y [n] of integers. For simplicity, assume that n is a power of 2. Problem is to design an algorithm that determines if there is a number p in X and a number q in Y such that p + q is zero. If such numbers exist, the algorithm returns true; otherwise, it returns false....