Find the maximum profit that can be generated for the given amount of money. As the answer can be rather large, report the answer as maxProfit % (10 ^ 9 + 7) where % denotes modulo operator.
Complete the function maxProfit in the editor.
maxProfit has the following parameters:
int[] cost: an array of integers representing the cost of each itemint x: the initial amount of money Walker hasReturns
int: the maximum profit that can be obtained modulo (10^9+7)
Examples
01 · Example 1
cost = [3, 4, 1] x = 8 return = 7
:)
02 · Example 2
cost = [19, 78, 27, 15, 20, 25] x = 25 return = 16
:)
03 · Example 3
cost = [10, 20, 14, 40, 50] x = 70 return = 20
Some possible combination of items Walker can buy are:
She can buy items 0, 1, and 2 for a cost of 44 and obtain a profit of 2 ^ 0 + 2 ^ 1 + 2 ^ 2 = 7
She can buy items 0 and 4 for a cost of 60 and obtain a profit of 2 ^ 0 + 2 ^ 4 = 17
She can buy items 1 and 4 for a cost of 70 and obtain a profit of 2 ^ 1 + 2 ^ 4 = 18
She can buy items 2 and 4 for a cost of 64 and obtain a profit of 2 ^ 2 + 2 ^ 4 = 20
Out of all the possible combinations, the maximum profit obtained is by buying items 2 and 4 for a cost of 64 to obtain a profit of 20.
Constraints
1 <= n <= 10^51 <= cost[i] <= 10^50 <= x <= 10^9
More IBM problems
- Count Ideal NumbersOA · Seen Jun 2026
- Count Descending SubarraysOA · Seen Apr 2026
- Count Power Products in RangeOA · Seen Apr 2026
- Minimum Operations to Make Alternating Binary StringSeen Feb 2026
- Minimum Number of Non-Empty Disjoint SegmentsSeen Feb 2026
- Count Unstable ProcessesOA · Seen Feb 2026
- Longest Balanced Binary SubarrayOA · Seen Feb 2026
- Process Execution TimeOA · Seen Nov 2025
public int maxProfit(int[] cost, int x) {
// write your code here
}
cost[3, 4, 1]
x8
expected7
sign in to submit