PSEUDO CODE:
def minimumLoss(price):
price_map = []
# price_map wil store records of (PRICE,
DAY)
for i in range(len(price)):
price_map.append([price[i], i])
# sort the price_map based on the PRICE
price_map.sort(key=lambda x:x[0])
bought_day = -1
sold_day = -1
currrent_profit = 0
i = 0
j = len(price_map) - 1
while i != j:
# if the sold day is greater than
bought date
if price_map[i][1] <
price_map[j][1]:
# compute
the profit
profit = abs(price_map[i][0] - price_map[j][0])
# if profit greater than current profit
if profit > current_profit:
current_profit = profit
bought_day = price_map[j][0]
sold_day = price_map[i][0]]
i += 1
j -= 1
return bought_day, sold_day
EXPLANATION:
First the given array A is considered and an extra storage
price_map is taken.
price_map contains records as (PRICE, DAY).
Then the price_map is sorted according to the price.
The difference is calculated from the extreme corners with a
condition such that the sold day should be after bought day.
This is done using the DAY attribute in the price_map
records.
The maximum profit day is considered and it is saved in bought_day
and sold_day and returned.
Here, the two key operations are sorting and linear
traversal.
So f(n) = sort + linear traversal, but sorting can be done in n log
n and linear traversal in n
implies, f(n) = n log n + n
Therefore O(n) = n log n.
(5 +2 points) You are given an array A[1..n] of positive numbers where Ai] is the stock price on ...
Design and analysis of algorithms
Type in answer
Problem 5. Given a sorted array of distinct integers A[1- -n], you want to find out whether there is an index I for which Ai-i. Give a divide-and-conquer algorithm that runs in time O(log n)
1. Design and write a Divide& Conquer algorithm that, given an array A of n distinct integers which is already sorted into ascending order, will find if there is some i such that Ali] in worst-case 0(log n) time.
ALGORITHM PROBLEM: A) Significant Inversions: We are given a sequence of n arbitrary but distinct real numbers <a1 , a2 ,..., an>. We define a significant inversion to be a pair i < j such that ai > 2 aj . Design and analyze an O(n log n) time algorithm to count the number of significant inversions in the given sequence. [Hint: Use divide-&-conquer. Do the “combine” step carefully] B) The Maximum-Sum Monotone Sub-Array Problem: Input: An array A[1..n] of...
need help in this algorithm question
Let A be an array containing n numbers (positive and negative). Develop a divide and conquer algorithm that finds the two indices 1 sisjsn such that A[k] (the sum of the elements from i to j) is maximized. For example, in the array A [10,-5,-6,5, 7,-2,4, -11], the sub-array A[4:6] has the sum 5+ 7-2+4-14 and no other sub-array contains elements that sum to a value greater than 14, so for this input the...
You are working for a small stock investment company that wants to look for patterns in optimal trading days in a given time period of n days. They want to find the best pair of days in a period of n days to buy a stock on the first day of the pair and sell it on the second day of the pair. That is, they want the biggest positive difference between the selling price on the second day and...
(13 pts) Given an array AlI,2,. .. ,n] integers, design and analyze an efficient Divide-and-Conquer algorithm to find some i and j, where j > 1, such that A[j]-Ali] is maximized. For example, given A 6, 1,3,8,4,5, 12,6], the maximum value of AL] - Ali] for j > i is 12-1 11 where j -7 and i 2. Give the underlying recurrence relation for your algorithm and analyze its running time. You should carefully state all details of your algorithm:...
1. (16 pts.) Sorted Array Given a sorted array A of n (possibly negative) distinct integers, you want to find out whether there is an index i for which Al = i. Give a divide-and-conquer algorithm that runs in time O(log n). Provide only the main idea and the runtime analysis.
Consider the problem where you are given an array of n digits
[di] and a positive integer
b, and you need to compute the value of the number in that
base.
In general, you need to compute
For example:
(1011)2 = 1(1) + 1(2) + 0(4) + 1(8) = 11;
(1021)3 = 1(1) + 2(3) + 0(9) + 1(27) = 34, and
(1023)4 = 3(1) + 2(4) + 0(16) + 1(64) = 75.
In these examples, I give the digits...
Suppose that we are given a sorted array of distinct integers A[1, ......, n] and we want to decide whether there is an index i for which A[i] = i. Describe an efficient divide-and-conquer algorithm that solves this problem and explain the time complexity. 1. Describe the steps of your algorithm in plain English. 2. Write a recurrence equation for the runtime complexity. 3. Solve the equation by the master theorem.
Suppose you are given an array A holding n distinct integers (negative values are allowed) in sorted order; in other words, A[i] < A[i + 1] for each i ∈ [0, n − 2]. We say the ith element is self referential if A[i] = i. Design an O(log n) time algorithm to determine if there is a self referencial element in the array. Your solution must include a) Statement of your algorithm in plain English. (Pseudo-code is optional.) b)...