Profits by cars

You are a car seller. You have N cars and the profit for each of the cars is given by an array P. The profit of cars are P1, P2, P3, ..., PN. Since you got a huge profit in the last month so you decide to get (N1) more sets of such cars. You already have one car. Now, you have N2 cars. Basically, there are N number of cars of each profit such as N cars for profit P1N cars of profit P2, and so on up to N cars of profit PN.

You can perform the following operations any number of times:

  • If the last car is sold for profit P, then you can sell a car for profit Pc>P .

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, N=4 and prices are P1, P2, P3, P4. Since N is 4, therefore you can have four sets of cars and the prices are P1, P2, P3, P4, P1, P2, P3, P4, P1, P2, P3, P4, P1, P2, P3, P4 .

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 = { 123 };
        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