Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You are given an array 'A' of length 'N' where the 'ith' element of 'A' represents the stock price of some company on 'ith' day.

You are given an array 'A' of length 'N' where the 'ith' element of 'A' represents the stock price of some company on 'ith' day. Initially, you have 'X' rupees to invest.

For each day from '1' to 'N', you have to perform the following procedure for investment:

For 'ith' day, you can use available rupees to buy or sell any number of stocks. Each stock you buy will require 'A[i]' amount of rupees, and for each stock you sell will add 'A[i]' rupees to your current amount of rupees.

Find the maximum amount of rupees you can have after 'N' days.

It is guaranteed that the answer for all the test cases fits in a 64-bit integer.

Example:

'N' = 4

'X' = 3

'A' = [3, 2, 3, 4]

On the first day, you will not buy or sell any stocks.

On the second day, you will buy '1' stock by spending '2' rupees, so you have '1' rupee and '1' stock at the end of the second day.

On the third day, you will not buy or sell any stocks.

On the fourth day, you will sell '1' stock, so you have '1 + 4 = 5' rupees and '0'

stocks at the end of the fourth day.

It can be proved that we can not have more than '5' rupees after the fourth day. So the answer for this case is '5'.

Detailed explanation (Input/output format, Notes, Images)

Input format: The first line of each test case contains two integers, 'N' and 'X', which denote the length of the array 'A' and the initial amount of rupees you have, respectively.

The second line of each test case contains 'N' space separated integers, the elements of the array 'A'.

Output format:

For each test case, return the maximum possible amount of rupees you can have after 'N' days.

Note:

You don't need to print anything. It has already been taken care of. Just implement the given function.

Sample input :

2

3 10

3 2 1

3 5

2 1 4 Sample output 1:

10

20

Explanation of sample Input 1:

For test case 1:

If you don't buy or sell any stock on any day, you will have '10' rupees at the end of the third day.

It can be proved that you can not have more than '10' rupees after the third day.

So the answer for this test case is '10'.

For test case 2:

On the first day, you will not buy or sell any stocks.

On the second day, you will buy '5' stock by spending '5' rupees, so you have '0' rupees and '5' stock at the end of the second day.

On the third day, you will sell '5' stocks at the price of '4' rupees, so you will

have '20' rupees and '0' stock after the third day. It can be proved that we can not have more than '20' rupees after the third day. So the answer for this case is '20'.

Def maxi(n,x,a):

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems An Application Oriented Approach Complete Version

Authors: Michael Kifer, Arthur Bernstein, Richard Lewis

2nd Edition

0321268458, 978-0321268457

More Books

Students also viewed these Databases questions