Maximize profit when divisibility by two numbers have associated profits
Last Updated :
28 Aug, 2023
Given five integers N, A, B, X and Y. The task is to find the maximum profit obtained from the numbers from the range [1, N]. If a positive number is divisible by A then the profit increases by X and if a positive number is divisible by B then the profit increases by Y.
Note: Profit from a positive number can be added at most once.
Examples:
Input: N = 3, A = 1, B = 2, X = 3, Y = 4
Output: 10
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.
Input: N = 6, A = 6, B = 2, X = 8, Y = 2
Output: 12
Naive approach: Iterate over all the numbers from 1 to N, and for each number check if it is divisible by A or B, and add the corresponding profit X or Y to the total profit. However, since profit can be added at most once, we also need to keep track of which numbers have already been considered and added to the profit.
Steps to implement the above approach:
- Initialize the total profit to 0.
- If i is divisible by A and has not already been considered, add X to the total profit and mark i as considered.
- If i is divisible by B and has not already been considered, add Y to the total profit and mark i as considered.
- Return the total profit.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxProfit( int n, int a, int b, int x, int y)
{
int profit = 0;
for ( int i = 1; i <= n; i++) {
if (i % a == 0 && i % b == 0) {
profit += max(x, y);
}
else if (i % a == 0) {
profit += x;
}
else if (i % b == 0) {
profit += y;
}
}
return profit;
}
int main()
{
int n = 6, a = 6, b = 2, x = 8, y = 2;
cout << maxProfit(n, a, b, x, y);
return 0;
}
|
Java
public class Main {
public static int maxProfit( int n, int a, int b, int x,
int y)
{
int profit = 0 ;
for ( int i = 1 ; i <= n; i++) {
if (i % a == 0 && i % b == 0 ) {
profit += Math.max(x, y);
}
else if (i % a == 0 ) {
profit += x;
}
else if (i % b == 0 ) {
profit += y;
}
}
return profit;
}
public static void main(String[] args)
{
int n = 6 , a = 6 , b = 2 , x = 8 , y = 2 ;
System.out.println(maxProfit(n, a, b, x, y));
}
}
|
Python3
def maxProfit(n, a, b, x, y):
profit = 0
for i in range ( 1 , n + 1 ):
if i % a = = 0 and i % b = = 0 :
profit + = max (x, y)
elif i % a = = 0 :
profit + = x
elif i % b = = 0 :
profit + = y
return profit
if __name__ = = '__main__' :
n = 6
a = 6
b = 2
x = 8
y = 2
print (maxProfit(n, a, b, x, y))
|
C#
using System;
public class Program {
public static int MaxProfit( int n, int a, int b, int x,
int y)
{
int profit = 0;
for ( int i = 1; i <= n; i++) {
if (i % a == 0 && i % b == 0) {
profit += Math.Max(x, y);
}
else if (i % a == 0) {
profit += x;
}
else if (i % b == 0) {
profit += y;
}
}
return profit;
}
public static void Main()
{
int n = 6, a = 6, b = 2, x = 8, y = 2;
Console.WriteLine(MaxProfit(n, a, b, x, y));
}
}
|
Javascript
function maxProfit(n, a, b, x, y) {
let profit = 0;
for (let i = 1; i <= n; i++) {
if (i % a === 0 && i % b === 0) {
profit += Math.max(x, y);
} else if (i % a === 0) {
profit += x;
} else if (i % b === 0) {
profit += y;
}
}
return profit;
}
const n = 6, a = 6, b = 2, x = 8, y = 2;
console.log(maxProfit(n, a, b, x, y));
|
Time Complexity: O(n) where n is the value of N provided in the input.
Auxiliary Space: O(1)
Approach: Easy to see that we can divide a number with both A and B only if the number is a multiple of lcm(A, B). Obviously, that number should be divided with the number that gives more profit.
So the answer equals to X * (N / A) + Y * (N / B) – min(X, Y) * (N / lcm(A, B)).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxProfit( int n, int a, int b, int x, int y)
{
int res = x * (n / a);
res += y * (n / b);
res -= min(x, y) * (n / ((a * b) / __gcd(a, b)));
return res;
}
int main()
{
int n = 6, a = 6, b = 2, x = 8, y = 2;
cout << maxProfit(n, a, b, x, y);
return 0;
}
|
Java
class GFG
{
static int __gcd( int a, int b)
{
if (b == 0 )
return a;
return __gcd(b, a % b);
}
static int maxProfit( int n, int a, int b,
int x, int y)
{
int res = x * (n / a);
res += y * (n / b);
res -= Math.min(x, y) * (n / ((a * b) / __gcd(a, b)));
return res;
}
public static void main (String[] args)
{
int n = 6 , a = 6 , b = 2 , x = 8 , y = 2 ;
System.out.println(maxProfit(n, a, b, x, y));
}
}
|
Python3
from math import gcd
def maxProfit(n, a, b, x, y) :
res = x * (n / / a);
res + = y * (n / / b);
res - = min (x, y) * (n / / ((a * b) / /
gcd(a, b)));
return res;
if __name__ = = "__main__" :
n = 6 ;a = 6 ; b = 2 ; x = 8 ; y = 2 ;
print (maxProfit(n, a, b, x, y));
|
C#
using System;
class GFG
{
static int __gcd( int a, int b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
static int maxProfit( int n, int a, int b, int x, int y)
{
int res = x * (n / a);
res += y * (n / b);
res -= Math.Min(x, y) * (n / ((a * b) / __gcd(a, b)));
return res;
}
static void Main()
{
int n = 6, a = 6, b = 2, x = 8, y = 2;
Console.WriteLine(maxProfit(n, a, b, x, y));
}
}
|
Javascript
<script>
function __gcd(a, b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
function maxProfit(n, a, b, x, y)
{
let res = x * Math.floor(n / a);
res += y * Math.floor(n / b);
res -= Math.min(x, y) * (n / ((a * b) / __gcd(a, b)));
return res;
}
let n = 6, a = 6, b = 2, x = 8, y = 2;
document.write(maxProfit(n, a, b, x, y));
</script>
|
PHP
<?php
function __gcd( $a , $b )
{
if ( $b == 0)
return $a ;
return __gcd( $b , $a % $b );
}
function maxProfit( $n , $a , $b , $x , $y )
{
$res = $x * ( $n / $a );
$res += $y * ( $n / $b );
$res -= min( $x , $y ) * ( $n /
(( $a * $b ) / __gcd( $a , $b )));
return $res ;
}
$n = 6;
$a = 6;
$b = 2;
$x = 8;
$y = 2;
print (maxProfit( $n , $a , $b , $x , $y ));
?>
|
Time Complexity: O(log(min(a, b)))
Auxiliary Space: O(log(min(a, b)))
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...