Question

In: Computer Science

A sequence of integers x1,x2,...,xn is unimodal if for some 1 ≤ i ≤ n we...

A sequence of integers x1,x2,...,xn is unimodal if for some 1 ≤ i ≤ n we have x1 < x2 < ... < xi and xi > xi+1 > ... > xn. Find the maximum element in a unimodal sequence of integers x1, x2, . . . , xn. The running time should be O(log n). Show that your algorithm meets the bound.

Solutions

Expert Solution

Maximum element in a unimodal sequence of integers x1<x2<......xi-1<xi>xi+1>...xn can be achieved through binary search with O(log n) time complexity

with mid element greater than adjacent elements then mid is the maximum.

In the diagram below you could see maximum can be found while iterating to right side of the tree. We do not require to iterate through each element. Hence it's complexity can be found with general O(log n) time complexity.

Below diagram there is code snippet, where you can also cross-check that you do not need to iterate through every element unless it is the worst case i.e. elements in an array in ascending order but that's not unimodal sequence. So it has time complexity of O(log n) for every case :)


Code Snippet

static int findMaxUnimodal(int arr[], int low, int high){

       if (low == high) //single element array condition
return arr[low];

       if ((high == low + 1) && arr[low] >= arr[high]) //two element scenario
return arr[low];

       if ((high == low + 1) && arr[low] < arr[high])
  return arr[high];

       int mid = (low + high)/2;   

       if ( arr[mid] > arr[mid + 1] && arr[mid] > arr[mid - 1])
  return arr[mid];

       if (arr[mid] > arr[mid + 1] && arr[mid] < arr[mid - 1])
  return findMaxUnimodal(arr, low, mid-1);

       else
  return findMaxUnimodal(arr, mid + 1, high);

    }


Related Solutions

Consider two sets of integers, X = [x1, x2, . . . , xn] and Y...
Consider two sets of integers, X = [x1, x2, . . . , xn] and Y = [y1, y2, . . . , yn]. Write two versions of a FindUncommon(X, Y ) algorithm to find the uncommon elements in both sets. Each of your algorithms should return an array with the uncommon elements, or an empty array if there are no uncommon elements. You do not have to write the ‘standard’ algorithms – just use them. Therefore, you should be...
Let X1 and X2 be uniform on the consecutive integers -n, -(n+1), ... , n-1, n....
Let X1 and X2 be uniform on the consecutive integers -n, -(n+1), ... , n-1, n. Use convolution to find the mass function for X1 + X2.
Consider n numbers x1, x2, . . . , xn laid out on a circle and some value α.
  Consider n numbers x1, x2, . . . , xn laid out on a circle and some value α. Consider the requirement that every number equals α times the sum of its two neighbors. For example, if α were zero, this would force all the numbers to be zero. (a) Show that, no matter what α is, the system has a solution. (b) Show that if α = 1/2 , then the system has a nontrivial solution. (c) Show...
Let x1 > 1 and xn+1 := 2−1/xn for n ∈ N. Show that xn is...
Let x1 > 1 and xn+1 := 2−1/xn for n ∈ N. Show that xn is bounded and monotone. Find the limit. Prove by induction
Let X1, X2, . . . , Xn be a random sample of size n from...
Let X1, X2, . . . , Xn be a random sample of size n from a Poisson distribution with unknown mean µ. It is desired to test the following hypotheses H0 : µ = µ0         versus     H1 : µ not equal to µ0 where µ0 > 0 is a given constant. Derive the likelihood ratio test statistic
Consider n numbers x1, x2, . . . , xn laid out on a circle and...
Consider n numbers x1, x2, . . . , xn laid out on a circle and some value α. Consider the requirement that every number equals α times the sum of its two neighbors. For example, if α were zero, this would force all the numbers to be zero. (a) Show that, no matter what α is, the system has a solution. (b) Show that if α = 1 2 , then the system has a nontrivial solution. (c) Show...
Let X1, X2, · · · , Xn (n ≥ 30) be i.i.d observations from N(µ1,...
Let X1, X2, · · · , Xn (n ≥ 30) be i.i.d observations from N(µ1, σ12 ) and Y1, Y2, · · · , Yn be i.i.d observations from N(µ2, σ22 ). Also assume that X's and Y's are independent. Suppose that µ1, µ2, σ12 , σ22  are unknown. Find an approximate 95% confidence interval for µ1µ2.
a real sequence xn is defined inductively by x1 =1 and xn+1 = sqrt(xn +6) for...
a real sequence xn is defined inductively by x1 =1 and xn+1 = sqrt(xn +6) for every n belongs to N a) prove by induction that xn is increasing and xn <3 for every n belongs to N b) deduce that xn converges and find its limit
Suppose we have a random sample of n observations {x1, x2, x3,…xn}. Consider the following estimator...
Suppose we have a random sample of n observations {x1, x2, x3,…xn}. Consider the following estimator of µx, the population mean. Z = 12x1 + 14x2 + 18x3 +…+ 12n-1xn−1 + 12nxn Verify that for a finite sample size, Z is a biased estimator. Recall that Bias(Z) = E(Z) − µx. Write down a formula for Bias(Z) as a function of n and µx. Is Z asymptotically unbiased? Explain. Use the fact that for 0 < r < 1, limn→∞i=1nri...
Let X1, X2, . . . , Xn iid∼ N (µ, σ2 ). Consider the hypotheses...
Let X1, X2, . . . , Xn iid∼ N (µ, σ2 ). Consider the hypotheses H0 : µ = µ0 and H1 : µ (not equal)= µ0 and the test statistic (X bar − µ0)/ (S/√ n). Note that S has been used as σ is unknown. a. What is the distribution of the test statistic when H0 is true? b. What is the type I error of an α−level test of this type? Prove it. c. What is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT