You are a car seller. You have cars and the profit for each of the cars is given by an array . The profit of cars are . Since you got a huge profit in the last month so you decide to get more sets of such cars. You already have one car. Now, you have cars. Basically, there are number of cars of each profit such as cars for profit , cars of profit , and so on up to cars of profit .
You can perform the following operations any number of times:
- If the last car is sold for profit , then you can sell a car for profit .
Note: You can select a car of any profit in the first operation as there are no cars that are sold earlier.
Find out the maximum profit that you can make.
For example, and prices are . Since is 4, therefore you can have four sets of cars and the prices are .
Example:
Input: n=3, nums = { 1, 2, 3 }
Output: 6
Approach
Java
import java.util.Arrays;public class ProfitsCars {public static void main(String[] args) {int size = 3;long sum = 0;int[] nums = { 1, 2, 3 };Arrays.sort(nums);sum += nums[0];for (int j = 1; j < nums.length; j++) {if (nums[j] != nums[j - 1]) {sum += nums[j];}}System.out.println(sum);}}
No comments:
Post a Comment