Question

You are given a sequence of positive real numbers a[1..n]. You can now add ‘+’ and...

You are given a sequence of positive real numbers a[1..n]. You can now add ‘+’ and ’×’ signs between these numbers, and your goal is to generate an expression that has the largest value. As an example, if a = {2, 3, 0.5, 2}, then you should output the expression 2 × 3 + 0.5 + 2 = 8.5. This is larger than any other expression (e.g. 2 × 3 × 0.5 × 2 = 6, 2 + 3 + 0.5 + 2 = 7.5, 2 + 3 ∗ 0.5 + 2 = 5.5 ...). You must add either a ‘+’ or a ‘×’ between two consecutive numbers, and you are not allowed to change the ordering of the numbers or add brackets. As usual the expression is evaluated to first compute the products and then the sum. Design an algorithm that runs in time O(n 2 ) and outputs the largest possible value. For this problem you can assume all additions, multiplications and comparisons of real numbers can be done in O(1) time. This needs to be in C++ and Please explain the process in comments. Thank you!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the C++ program:

#include <iostream>

using namespace std;

int main()
{
int n; // to input the total numbers in the array

cout<<"Enter the number of Numbers in the array: ";
cin>>n;

double arr[n];// array to store the numbers

cout<<"Enter the numbers\n";
  
for(int i=0;i<n;i++)
{
cin>>arr[i]; // stores each element on ith position i.e. [0....n-1]
}
  
double ans=0; // Initialize our answer with 0
  
/* Logic is to find all the numbers which are greater than 1 and find there continues multiplication first,
as 1+1=2 and 1*1=1 this means that the no. which are less than or equal to 1 will always give
greater value in addition than in multiplication
*/
for(int i=0;i<n-1;i++)// as we are comparing two adjacent values therefore loop will run till [0...n-2]  
{
if(arr[i]>1 && arr[i+1]>1)
{
int temp=arr[i]*arr[i+1]; // stores multiple of two numbers adjacent to each other

arr[i]=-1;// replaces previous value to -1 so that we won't traverse it again
arr[i+1]=temp;// stores continuous multiplication value to the latest array value
// so it will be used in our next iteration
  
}
  
}
/* now according to the example the modified array will be like [-1,6,0.5,2], now we have to add all the numbers
which are not -1*/
  
for(int i=0;i<n;i++)
{
if(arr[i]!=-1)
{
ans+=arr[i]; // ans stores sum of all the values of the array
}
}

cout<<"The largest number possible is: "<<ans;
return 0;
}

OUTPUT:

The complexity is O(n2) as we are iterating loop two times, first to find continues multiple and then to find sum of all the numbers.

Add a comment
Know the answer?
Add Answer to:
You are given a sequence of positive real numbers a[1..n]. You can now add ‘+’ and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Given are n real numbers x(1), x(2), ..., x(n). Some of them are positive, some may...

    Given are n real numbers x(1), x(2), ..., x(n). Some of them are positive, some may be negative. The total sum is positive. Prove the following statement: There exists some index i such that all the following n sums are positive: х() x()xi+1) x() x+1)xi+2) х() + x(+1) + x(і+2) + ... х(і+n-1) Here "plus" and "minus" within the brackets are meant modulo n. Given are n real numbers x(1), x(2), ..., x(n). Some of them are positive, some may...

  • The sequence (Un) of positive real numbers satisfies the relationship In-1XnXn+1 = 1 for all n...

    The sequence (Un) of positive real numbers satisfies the relationship In-1XnXn+1 = 1 for all n > 2. If x1 = 1 and x2 = 2, what are the values of the next few terms? What can you say about the sequence? What happens for other starting values? The sequence (yn) satisfies the relationship Yn-1Yn+1 + Yn = 1 for all n > 2. If y1 = 1 and Y2 = 2, what are the values of the next few...

  • 1. Let {n} be a sequence of non negative real numbers, and suppose that limnan =...

    1. Let {n} be a sequence of non negative real numbers, and suppose that limnan = 0 and 11 + x2 + ... + In <oo. lim sup - n-00 Prove that the sequence x + x + ... + converges and determine its limit. Hint: Start by trying to determine lim supno Yn. What can you say about lim infn- Yn? 3 ) for all n Expanded Hint: First, show that given any e > 0 we have (...

  • an+1 for all values of n. What 1. Let {an} be a sequence of positive, real...

    an+1 for all values of n. What 1. Let {an} be a sequence of positive, real numbers such that is lim an? Explain how you got your answer. an 3n + 1 n-> 2. Let {an} and {bn} each be a sequence of positive real numbers. You know that ) bn converges and k=1 21. Your buddy Ron concludes that the series converges also. Select an item below and n70 bm 10. explain. lim An _ 1001

  • 2) Given an array of n nonzero real numbers a[0]…a[n-1], write a function to partition the...

    2) Given an array of n nonzero real numbers a[0]…a[n-1], write a function to partition the array (not sort) so that all its negative elements come before all its positive elements. Your algorithm should have O(n) time complexity. The function prototype is void negpospartition(float a[], int n) Please use C language

  • (1) Let a (.. ,a-2, a-1,ao, a1, a2,...) be a sequence of real numbers so that f(n) an. (We may eq...

    (1) Let a (.. ,a-2, a-1,ao, a1, a2,...) be a sequence of real numbers so that f(n) an. (We may equivalently write a = (abez) Consider the homogeneous linear recurrence p(A)/(n) = (A2-A-1)/(n) = 0. (a) Show ak-2-ak-ak-1 for all k z. (b) When we let ao 0 and a 1 we arrive at our usual Fibonacci numbers, f However, given the result from (a) we many consider f-k where k0. Using the Principle of Strong Mathematical Induction slow j-,-(-1...

  • Note: when a problem has an array of real numbers, you cannot use counting sort or...

    Note: when a problem has an array of real numbers, you cannot use counting sort or radix sort. ​​​​​​​ For this problem, you are given a number t and an array A with n real numbers that are not sorted. Describe an algorithm that finds the t numbers in A that are closest to the median of A. That is, if A = {x1, . . . , xa) and xrn is the median, we want to find the t...

  • Problem 1. Given a polynorial p(x) anz" + an-lz"-ı + + aix + ao, where the...

    Problem 1. Given a polynorial p(x) anz" + an-lz"-ı + + aix + ao, where the coefficients are a,'s, Horner's method is an efficient algorithm for evaluating the polynomial at a number c that works as follows: Multiply an by c, then add an-1. Then multiply the result by c and add an-2. Then multiply the result by c and add an-3 and so on until you reach a0. This over all gives an O(n) algorithm for evaluation of p(c)...

  • (Q2) Calculating the Distribution of Values 10 marks For a set of n real numbers {xi,...,...

    (Q2) Calculating the Distribution of Values 10 marks For a set of n real numbers {xi,..., x.J, here are a number of basic statistics we would like to know about the distribution of these numbers: ·The minimum value m = min(x 1, . .., Xn The maximum value Mmax{xi,..., x,J The average value a -^ 1x The standard deviation sx,-a)2. Write a program stats.c that calculates these values for a sequence of values it reads from stdin. The input is...

  • Algorithm Humble Numbers Problem 4-1 (Humble Numbers) 10 points A number k > 1 is called...

    Algorithm Humble Numbers Problem 4-1 (Humble Numbers) 10 points A number k > 1 is called humble if the only prime factors of k are 3 and 5. Consider the task of on input n, outputting the n smallest humble numbers and the following algorithm to do it: HuMBLE(n) count = 0. pret,Output = 0 HEAP.INSERT (3) HEAP.INSERT (5) while (count < n) cur - HEAP. EXTRACTMIN if cur prevOutput then output cur HEAP,INSERT(3*cur) HEAP.İNSERT(5*cur) count -count+1 preu,Output = cur...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT