Problem · Array

Profitable Project Pairs

Learn this problem
MediumAkuna Capital logoAkuna CapitalINTERNOA

Problem statement

A team can choose any two distinct projects from n available projects.

For project i:

  • profit[i] is its expected profit.
  • implementationCost[i] is its implementation cost.

A pair of projects (i, j), where 0 <= i < j < n, is profitable when:

(profit[i] - implementationCost[i]) + (profit[j] - implementationCost[j]) > 0

Return the number of profitable project pairs.

Complete the function getProfitablePairs, which accepts the arrays profit and implementationCost and returns the required count.

Function

getProfitablePairs(profit: int[], implementationCost: int[]) → long

Examples

Example 1

profit = [2, 3, 7, 1, 1]implementationCost = [3, 4, 5, 1, 2]return = 4

The net profits are [-1, -1, 2, 0, -1].

The profitable pairs are:

  • (0, 2): -1 + 2 = 1
  • (1, 2): -1 + 2 = 1
  • (2, 3): 2 + 0 = 2
  • (2, 4): 2 + (-1) = 1

No other pair has a positive combined net profit, so the result is 4.

Constraints

  • 1 <= n <= 2 * 10^5
  • profit.length = implementationCost.length = n
  • 1 <= profit[i], implementationCost[i] <= 10^9

More Akuna Capital problems

drafts saved locally
public long getProfitablePairs(int[] profit, int[] implementationCost) {
  // Write your code here.
}
profit[2, 3, 7, 1, 1]
implementationCost[3, 4, 5, 1, 2]
expected4
checking account