Maximize count of distinct profits possible by N transactions
Given three integers N, X, and Y, representing the total number of transactions, minimum and maximum profit respectively, the task is to find the count of distinct total profits that can be earned in N ( N > 1) transactions using X and Y at least once.
Examples:
Input: N = 3, X = 13, Y = 15
Output: 3
Explanation:
The different possible transactions satisfying the conditions are as follows:
- In first two transactions, the profit earned is 13. In the last transaction, the profit earned is 15. Therefore, the total profit earned = 13 + 13 + 15 = 41.
- In first two transactions, the profit earned is 13 and 14 respectively. In the last transaction, the profit earned is 15. Therefore, the total profit earned = 13 + 14 + 15 = 42.
- In first transaction, profit earned is 13. In the last two transactions, profit earned is 15. Therefore, the total profit = 13 + 15 + 15 = 43.
Therefore, the total distinct profits earned is 3.
Input: N = 2, X = 10, Y = 17
Output: 1
Approach: The given problem can be solved based on the following observations:
- The minimum total profit that can be earned is:
- The maximum total profit that can be earned is:
- Now it can be observed that all the total profit will lie within the range [S1, S2].
Follow the steps below to solve the problem:
- Print the value (S2-S1+1).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int numberOfWays( int N, int X, int Y)
{
int S1 = (N - 1) * X + Y;
int S2 = (N - 1) * Y + X;
return (S2 - S1 + 1);
}
int main()
{
int N = 3;
int X = 13;
int Y = 15;
cout << numberOfWays(N, X, Y);
return 0;
}
|
Java
import java.util.Arrays;
class GFG
{
static int numberOfWays( int N, int X, int Y)
{
int S1 = (N - 1 ) * X + Y;
int S2 = (N - 1 ) * Y + X;
return (S2 - S1 + 1 );
}
public static void main(String[] args)
{
int N = 3 ;
int X = 13 ;
int Y = 15 ;
System.out.println(numberOfWays(N, X, Y));
}
}
|
Python3
def numberOfWays(N, X, Y):
S1 = (N - 1 ) * X + Y
S2 = (N - 1 ) * Y + X
return (S2 - S1 + 1 )
if __name__ = = '__main__' :
N = 3
X = 13
Y = 15
print (numberOfWays(N, X, Y))
|
C#
using System;
class GFG
{
static int numberOfWays( int N, int X, int Y)
{
int S1 = (N - 1) * X + Y;
int S2 = (N - 1) * Y + X;
return (S2 - S1 + 1);
}
public static void Main()
{
int N = 3;
int X = 13;
int Y = 15;
Console.WriteLine(numberOfWays(N, X, Y));
}
}
|
Javascript
<script>
function numberOfWays( N, X, Y)
{
let S1 = (N - 1) * X + Y;
let S2 = (N - 1) * Y + X;
return (S2 - S1 + 1);
}
let N = 3;
let X = 13;
let Y = 15;
document.write(numberOfWays(N, X, Y));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
07 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...