# Maximize the total profit of all the persons X in Java

We are given 5 Integer variables Num, P1, P2, profit_P1, profit_P2 and the task is to maximize the profit and from all the natural numbers in the range of [1,Num]. The approach here is that if a positive number is divisible by P1 the profit increases by profit_P1 and similarly if if the number in the range is divisible by P2 the profit margin of profit_P2 increases. Also, the profit from a positive integer can be added at most once.

## Let us understand with example:-

Input − int num = 4, P1 = 6, P2 = 2, profit_P1 = 8, profit_P2 = 2;

Output − Maximize the total profit of all the persons X 4

Explanation −Here we have number ranged from 1 to 4([1,Num(4)])

No number in the series is divisible by P1

1 and 2 is divisible by P2

1 and 2 is divisible by P2 to get the profit of 2*2=4

Input − num = 3, P1 = 1, P2 = 2, profit_P1 = 3, profit_P2 = 4

Output − Maximize the total profit of all the persons X 10

Explanation − 1, 2 and 3 are divisible by A.

2 is the only number in the given range which is divisible by B.

2 is divisible by both A and B.

1 and 3 can be divided by A to get the profit of 2 * 3 = 6

2 can be divided by B to get the profit of 1 * 4 = 4

2 is divisible by both but in order to maximise the profit it is divided by B instead of A.

## Approach used in the below program is as follows −

• We are been given with 6 integer variables that includes the positive number range(Num), P1 which depicts the first person, P2 depicts the second person,profit_P1 which resembles the profit of person 1(i.e. if the number in the given number range is divisible by P1 profit_P1 rises), and similarly profit_P2.

• Inside the main function a method is called(profitMaximisation) which is the utility method for all the calculation.

• Inside the function it can be seen that every number which is divisible with both P1 and P2 only if the number is a multiple of the LCM of either P1 or P2.Also it should be divided with the number that gives more profit.

• So here it is calculated by profit_P1 * (num / P1) + profit_P2 * (num / P2) – min(profit_P1, profit_P2) * (num / lcm(P1, P2)).

• A method is introduced CalculateGcd() to calculate the lcm of the given numbers.

• The final output is captured in the main method and displayed to user as output.

## Example

public class testClass{
static int CalculateGcd(int n1, int n2){
if (n2 == 0)
return n1;
return CalculateGcd(n2, n1 % n2);
}
static int profitMaximisation(int n, int a, int b, int x, int y){
int result = x * (n / a);
result += y * (n / b);
result -= Math.min(x, y) * (n / ((a * b) / CalculateGcd(a, b)));
return result;
}
public static void main(String[] args){
int num = 6, P1 = 6, P2 = 2, profit_P1 = 8, profit_P2 = 2;
System.out.println("Maximize the total profit of all the persons X "+profitMaximisation(num, P1, P2, profit_P1, profit_P2));
}
}

## Output

If we run the above code it will generate the following Output

Maximize the total profit of all the persons X 12

Updated on: 05-Nov-2021

206 Views 