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⁹




22 1
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1
#includeusing namespace std;

int stockPairs(int stocksProfit[],int target, int n){
    sort(stocksProfit, 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){ // continue while low < high
        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 less than target, then increment low so that sum increases.
        }
        else{
            high -= 1; // if sum is greater than target, then decrement high so that sum decreases.
        }
    }
    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<<stockPairs(stocksProfit, target, n)<<endl; // find distinct pairs
}
Add a comment
Know the answer?
Add Answer to:
A financial analyst is responsible for a portfolio of profitable stocks represented in an array.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT