Home>

### python : I can not come up with a task algorithm

Condition: Mr. Twister, banker and financier, has access to secret materials -a local cash currency rate on N days forward. As a person, everywhere trying to find benefits, Mr. Twister set himself a task -you need to choose a day when you need to buy this currency and when it needs to be sold to get the maximum profit.

Input format: The first line introduces the number N -the number of days to which the currency rate is known. In the second line there is n numbers -the cost of the currency in rubles to the appropriate day.

Output format: It is required to withdraw two numbers -a day when you need to buy a currency and a day when you need to sell it to get the maximum profit. Moreover, if you choose this a couple of days, you need to choose the one at which Mr. Twister will be able to sell currency from hand. If there are several such pairs, then you need to consider the same principle -the better the better. If you buy and sell currency in these days, it will be unprofitable or useless, then output -1.

Example: Input: five 1 2 1 5 3 Output: 3 4.

## Enter: 3. 200 100 50. Conclusion:

Extinguits of my reasoning: I realized only that if the next number is the next number of the previous one, the stocks fall and need to withdraw -1. (0 Added to avoid index Out of Range)

``````n= int(input())
term= list(map(int, input().split()))
term.append(0)
if not any(term[i] < term[i + 1] for i in range(n)):
print(-1)
``````

Next, just fall into a stupor. The task seems familiar, perhaps its condition is popular at the Olympics. However, what algorithm? If you go throughout the list and iterator to track when the next number is smaller, and when there is more than the previous one, it is for a long time, in memory, and not the fact that logic will work. There was an option to search for the increasing indices of the maximum and minimum number to compare their position, but if the maximum is before the minimum, what's next? If someone came across an analogue, can you explain the principle, please?

Dumb algorithm in the forehead: for each couple of days I and J (i Roman Konoval2021-05-06 07:05:16

Correct the header to the problem.

MBo2021-05-05 14:10:58

In the task, it is never mentioned that it is necessary to buy before selling. Is it implied or not?

Stanislav Volodarskiy2021-05-05 14:26:37