- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# Maximize profit when divisibility by two numbers have associated profits in C++

We are given with five integers **N, A, B, X **and **Y**. The goal is to maximize the profit by checking that between numbers in range [ 1 to N ] , if

A number is divisible by A, then profit increases by

**X**.A number is divisible by B then profit increases by

**Y**.

A profit can be added once only, for a particular number in range.

Let’s understand with examples.

**Input** − N=4, A=2, B=3, X=2, Y=3

**Output** − Maximized profit is − 7

**Explanation** −

2,4 are divisible by A ( 2 ). Profit increases from 0 to 2, then 2 to 4 ( by X=2 )

3 is divisible by B ( 3 ). Profit increases from 4 to 7. (by Y=3)

**Input** − N=5, A=2, B=4, X=1, Y=3

**Output** − Maximized profit is: 4

**Explanation** −

2,4 are divisible by A ( 2 ).

4 is also divisible by B ( 4 ).

For 2 profit increases from 0 to 1 (by X). For 4 we chose divisibility by B. So profit increases by Y not X as Y is more. So it rises from 1 to 4 ( by Y=3 ).

## Approach used in the below program is as follows

We have integers N, A, B, X, Y.

Function maximizeProfit(int n, int a, int b, int x, int y) calculates the profit and returns the value. It takes all variables as parameters and return maximized profit.

Variable profit will have an amount of profit, initially 0.

Starting from 1 to n, check divisibility of i by a and b, using for loop

If i is divisible by both a and b then increase profit by x or y whichever is more.

Else if i is divisible by a only, increase profit by x

Else if i is divisible by b only, increase profit by y

At the end return value present in profit as result.

## Example

#include <bits/stdc++.h> using namespace std; // Function to return the maximum profit int maximizeProfit(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){ int maxx=x>=y?x:y; profit+=maxx; } else if(i%a==0){ profit+=x; } else if(i%b==0){ profit+=y; } } return profit; } int main(){ int N = 6, A = 2, B =4, X = 6, Y = 3; cout <<"Maximized profit is: "<<maximizeProfit(N,A,B,X,Y); return 0; }

## Output

If we run the above code it will generate the following output −

Maximized profit is: 2

- Related Articles
- Maximize the profit by selling at-most M products in C++
- Numbers obtained during checking divisibility by 7 using JavaScript
- Maximize number of continuous Automorphic numbers in C++
- Maximize the total profit of all the persons X in Java
- Maximize array elements up to given numbers in C++
- Divisibility by which number is understood by checking the last two digits?
- Maximize the number by rearranging bits in C++
- Add two numbers represented by two arrays in C Program
- Divisibility by 12 for a large number in C++ Program
- Check if a large number is divisibility by 15 in C++
- Divisibility by 64 with removal of bits allowed in C++ Program
- Test the divisibility of the following numbers by 6 a) 5634 b) 65990
- Multiply two numbers represented by Linked Lists in C++
- How To Do Divisibility In Odd Or Even Numbers
- Maximize number of 0s by flipping a subarray in C++