A financial analyst is responsible for a portfolio of profitable stocks represented in an array.
The following solution is suggested to handle the subject “A financial analyst is responsible for a portfolio of profitable stocks represented in an array.“. Let’s keep an eye on the content below!
Question “A financial analyst is responsible for a portfolio of profitable stocks represented in an array.”
code in c++
2. Profit Targets
A financial analyst is responsible for a portfolio of profitable stocks represented in an array. Each item in the array represents the yearly profit of a corresponding stock. The analyst gathers all distinct pairs of stocks that reached the target profit. Distinct pairs are pairs that differ in at least one element. Given the array of profits, find the number of distinct pairs of stocks where the sum of each pair’s profits is exactly equal to the target profit.
Example
stocksProfit =[5,7,9,13,11,6,6,3,3]
target =12 profit’s target
– There are 4 pairs of stocks that have the sum of their profits equals to the target 12. Note that because there are two instances of 3 in stocksprofit there are two pairs matching (9,3) : stocksprofits indices 2 and 7, and indices 2 and 8 , but only one can be included.
– There are 3 distinct pairs of stocks: (5,7), 3,9), and (6,6) and the return value is 3 .
Function Description
Complete the function stockPairs in the editor below.
stockPairs has the following parameter(s):
int stocksProfit[n]: an array of integers representing the stocks profits target: an integer representing the yearly target profit
Returns:
inc the total number of pairs determined
Constraints
– 1 ≤ n ≤ 5 × 10⁵
– 0 ≤ stockspront(i) ≤ 10⁹
– 0 ≤ target ≤ 5 × 10⁹
Answer
{#includeusing namespace std; int stockPairs(int stocksProfit[],int target, int n)#includeusing namespace: int stockPairs (int stocksProfit[],int Target, int N)sort(stocksProfit+n); // Sort the stocksProfit array. {int low = 0, high = n-1; // low and high represent two pointers int pairs = 0; // count of all distinct pairs while(low < high)int low = 0, and high = 1; // Low and High are two pointers. Other than that, high = n-1; // low and high are two pointers int pairs = 0; // continue while low and high are equal if(stocksProfit[low] + stocksProfit[high] == target) // find equal then increment pairs pairs += 1: low += 1 ; high -=1 ; else if(stocksProfit[low] + stocksProfit[high] == target) low += 1? // If sum is greater than the sum, decrement high to decrease the sum. } return pairs; // return distinct pairs. } int main(){ int target = 12; // the target int stocksProfit[] = 5,7,9,13,11,6,6,3,3; // the stockProfits array int n = sizeof(stocksProfit)/sizeof(stocksProfit[0]); // n - represents size of array cout<
Conclusion
Above is the solution for "A financial analyst is responsible for a portfolio of profitable stocks represented in an array.". We hope that you find a good answer and gain the knowledge about this topic of engineering.