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...
(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...
Let A = [A[1], A[2],…..,A[n]] be an array of n distinct integers. For 1 <= j <= n, the index j is a happy index if A[i] < A[j] for all 1 <= i < j. Describe an O(n)- time algorithm that finds all the happy indices in the array A. Partial credit will be given for an O(n log(n))-time algorithm and a minimal credit will be given for an O(n^2) –time algorithm. What is the running time of your...
Suppose you have an array S indexed from 1 to n which contains n numbers, not in any particular order, and you wish to count how many times a given number x occurs in S. Consider the recursive algorithm below for this which finds the number of occurrences of x in the index range i...j in S. Of course, solving the problem would involve an initial call to the algorithm for the range 1.n: int CountOccur (int i,j) { int...
the question from the course COMP 4040 that Analysis of Algorithms if you want to answer it by code please use C or C++ 5. Algorithm Design (20 points) Input: array A contains n distinct numbers from 1 to n, in arbitrary order. Output: number of inversions (defined as the number of pair(i, j) of array indices with i < j and A[i] > Aj]) (a) (5 points) What array with elements from the set {1, 2, ..., n) has...