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:
- S1 = (N-1)*X +Y.
- The maximum total profit that can be earned is:
- S2 = (N-1)*Y + X.
- 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++
// C++ program for the above approachÂ
#include <bits/stdc++.h>using namespace std;Â
// Function to count distinct// profits possibleint numberOfWays(int N, int X, int Y){    // Stores the minimum total profit    int S1 = (N - 1) * X + Y;Â
    // Stores the maximum total profit    int S2 = (N - 1) * Y + X;Â
    // Return count of distinct profits    return (S2 - S1 + 1);}Â
// Driver codeint main(){    // Input    int N = 3;    int X = 13;    int Y = 15;Â
    // Function call    cout << numberOfWays(N, X, Y);    return 0;} |
Java
// Java program for the above approachÂ
import java.util.Arrays;Â
class GFG{Â
// Function to count distinct// profits possiblestatic int numberOfWays(int N, int X, int Y){    // Stores the minimum total profit    int S1 = (N - 1) * X + Y;Â
    // Stores the maximum total profit    int S2 = (N - 1) * Y + X;Â
    // Return count of distinct profits    return (S2 - S1 + 1);}Â
Â
// Driver codepublic static void main(String[] args){    // Input    int N = 3;    int X = 13;    int Y = 15;Â
    // Function call    System.out.println(numberOfWays(N, X, Y));    }}Â
// This code is contributed by jana_sayantan. |
Python3
# Python3 program for the above approachÂ
# Function to count distinct# profits possibledef numberOfWays(N, X, Y):         # Stores the minimum total profit    S1 = (N - 1) * X + YÂ
    # Stores the maximum total profit    S2 = (N - 1) * Y + XÂ
    # Return count of distinct profits    return (S2 - S1 + 1)Â
# Driver codeif __name__ == '__main__':         # Input    N = 3    X = 13    Y = 15Â
    # Function call    print(numberOfWays(N, X, Y))     # This code is contributed by SURENDRA_GANGWAR |
C#
// C# program for the above approachusing System;Â Â Â Â Â Â Â Â Â class GFG{Â
// Function to count distinct// profits possiblestatic int numberOfWays(int N, int X, int Y){    // Stores the minimum total profit    int S1 = (N - 1) * X + Y;Â
    // Stores the maximum total profit    int S2 = (N - 1) * Y + X;Â
    // Return count of distinct profits    return (S2 - S1 + 1);}     // Driver Codepublic static void Main(){    // Input    int N = 3;    int X = 13;    int Y = 15;Â
    // Function call    Console.WriteLine(numberOfWays(N, X, Y));     }}Â
// This code is contributed by code_hunt. |
Javascript
<script>Â
// JavaScript program for the above approachÂ
// Function to count distinct// profits possiblefunction numberOfWays( N, X, Y){    // Stores the minimum total profit    let S1 = (N - 1) * X + Y;Â
    // Stores the maximum total profit    let S2 = (N - 1) * Y + X;Â
    // Return count of distinct profits    return (S2 - S1 + 1);}Â
// Driver code    // Input    let N = 3;    let X = 13;    let Y = 15;Â
    // Function call    document.write(numberOfWays(N, X, Y));     // This code is contributed by shivanisinghss2110Â
</script> |
Output:Â
3
Â
Time Complexity: O(1)
Auxiliary Space: O(1)
Â
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



