Profit Target: Given an array of n elements where each element gives the yearly profit of a corresponding stock, find the max number of distinct pairs (pairs differing in at least one element) from the array that adds up to the given profit target.
Sample Input 1:
[1,3,46,1,3,9] , sum = 47
possible pairs are: (1,46), (46,1), (46,1), (1,46)
indexes: [0][2] [2][0] [2][3] [3][2]
- four times due to repetition of '1'
- return only once - (46,1)
Hence, output = 1
Sample Input 2:
[5,7,9,13,11,6,6,3,3] sum = 12
possible pairs are: (5,7), (7,5), (9,3),
(9,3), (3,9), (9,3),
(6,6), (6,6)
indexes: [0][1] [1][0] [2][7] [2][8]
[7][2] [8][2] [5][6] [6][5]
return only distinct - (5,7), (9,3), (6,6)
Hence, output = 3
Simple Max Difference: Given an array of size n where elements are closing prices of a particular stock on n days in order, find the maximum profit spread of the stock. Profit spread it the difference between the closing price of a day and the closing price on any of the previous days in history. If the prices remain flat or are continuously decreasing, return -1.
Sample input 1:
[7,1,2,5]
1<7 -> continue;
2>1 -> 2-1 = 1 : diff
5>2 -> 5-2 = 3
5-1 = 4 : maximum diff = output
Sample Input 2:
[7,5,2,1]
5<7 , 2<5 , 1<2 -> continuously decreasing : return -1;