Question

In: Computer Science

Let X = {x1,x2,...,xn} a sequence of real numbers. Design an algorithm that in linear time...

Let X = {x1,x2,...,xn} a sequence of real numbers. Design an algorithm that in linear time finds the continue subsequence of elements xi,xi+1,...,x, which product is the maximum. Suppose that the product of an empty subsequence is 1 and observe that the values can be less to 0 and less to 1.

Solutions

Expert Solution

#include <bits/stdc++.h>

using namespace std;

int max_product(int a[], int n)

{

int max_ending=1, min_ending=1;

int max_sofar=1, flag=0;

for(int i=0;i<n;i++)

{

//if the element is positive then multiply max_ending with the element

//in case min_ending is already negetive, multiplying it with positive element makes it even smaller

//flag is to mark that at least one positive value exist in the array

if(a[i]>0)

{

max_ending=max_ending*a[i];

min_ending=min(min_ending*a[i],1);

flag=1;

}

//if the element is zero the initialize the variables to 1 again since we have to find the continous sequence

//so we will be looking for a new subsequence from here

else if(a[i]==0)

{

max_ending=1;

min_ending=1;

}

//if the element is negetive then in case min_ending is negetive we multiply it with a[i] to get the positive product

//else one will be assigned to max_ending

//min_ending is then multiplied with the older value of max_ending(that was positive obviously) to get even smaller value---

//---since a[i] is negetive

else

{

int temp=max_ending;

max_ending=max(min_ending*a[i],1);

min_ending=temp*a[i];

}

//update max_sofar after each operation if required

if(max_sofar<max_ending)

max_sofar=max_ending;

}

//if there is an empty subsequence the returen 1. (As per the questions condition)

//(this can be modified to other condition as per requirement)

//else return max_sofar

if(flag==0 && max_sofar==1)

return 1;

return max_sofar;

}

int main()

{

cout<<"Enter the size of the array: ";

int n;

cin>>n;

int a[n];

cout<<"\nEnter the elements in the array: ";

for(int i=0;i<n;i++)

cin>>a[i];

cout<<"\nMax subarray product is: "<<max_product(a,n)<<endl; //call the max_product function to find the subarray with max product

return 0;

}


Related Solutions

Let X = ( X1, X2, X3, ,,,, Xn ) is iid, f(x, a, b) =...
Let X = ( X1, X2, X3, ,,,, Xn ) is iid, f(x, a, b) = 1/ab * (x/a)^{(1-b)/b} 0 <= x <= a ,,,,, b < 1 then, find a two dimensional sufficient statistic for (a, b)
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
: Let X1, X2, . . . , Xn be a random sample from the normal...
: Let X1, X2, . . . , Xn be a random sample from the normal distribution N(µ, 25). To test the hypothesis H0 : µ = 40 against H1 : µne40, let us define the three critical regions: C1 = {x¯ : ¯x ≥ c1}, C2 = {x¯ : ¯x ≤ c2}, and C3 = {x¯ : |x¯ − 40| ≥ c3}. (a) If n = 12, find the values of c1, c2, c3 such that the size of...
. Let X1, X2, . . . , Xn be a random sample from a normal...
. Let X1, X2, . . . , Xn be a random sample from a normal population with mean zero but unknown variance σ 2 . (a) Find a minimum-variance unbiased estimator (MVUE) of σ 2 . Explain why this is a MVUE. (b) Find the distribution and the variance of the MVUE of σ 2 and prove the consistency of this estimator. (c) Give a formula of a 100(1 − α)% confidence interval for σ 2 constructed using the...
Let {xn} be a real summable sequence with xn ≥ 0 eventually. Prove that √(Xn*Xn+1) is...
Let {xn} be a real summable sequence with xn ≥ 0 eventually. Prove that √(Xn*Xn+1) is summable.
Let estimator π(hat) = X(bar) for X1, X2, . . . , Xn, Xi ∼ Bernoulli(π)...
Let estimator π(hat) = X(bar) for X1, X2, . . . , Xn, Xi ∼ Bernoulli(π) Recall: P(X = x) = πx (1 − π)1−x , x ∈ {0, 1} E(X) = π V(X) = π(1 − π) a. Show that π(hat) is a Consistent estimator of π b. Find the Maximum Likelihood Estimator of π c. Show that π(hat) is a Minimum Variance Unbiased Estimator of π Please explain the answer in detail
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 be a random sample from the uniform distribution with...
Let X1,X2, . . . , Xn be a random sample from the uniform distribution with pdf f(x; θ1, θ2) = 1/(2θ2), θ1 − θ2 < x < θ1 + θ2, where −∞ < θ1 < ∞ and θ2 > 0, and the pdf is equal to zero elsewhere. (a) Show that Y1 = min(Xi) and Yn = max(Xi), the joint sufficient statistics for θ1 and θ2, are complete. (b) Find the MVUEs of θ1 and θ2.
Let X1, X2, . . . , Xn be iid following a uniform distribution over the...
Let X1, X2, . . . , Xn be iid following a uniform distribution over the interval (θ, 2θ) (θ > 0). (a) Find a method of moments estimator of θ. (b) Find the MLE of θ. (c) Find a constant k such that E(k ˆθ) = θ. (d) By using the Rao-Blackwell, which estimators of (a) and (b) can be improved?
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT