Count ways to divide C in two parts and add to A and B to make A strictly greater than B

Given three integers A, B and C, the task is to count the number of ways to divide C into two parts and add to A and B such that A is strictly greater than B.Â
Examples:Â
Â
Input: A = 5, B = 3, C = 4Â
Output: 3Â
The possible values of A and B after dividing C are:Â
A = 7, B = 5 where C is divided into 2 and 2.Â
A = 8, B = 4 where C is divided into 3 and 1.Â
A – 9, B = 3 where C is divided into 4 and 0.
Input: A = 3, B = 5, C = 5Â
Output: 2Â
Â
Â
Approach: On observing carefully, the following relation is formed for this problem.Â
Â
- Let addA and addB be added to A and B respectively.
- Therefore, addA + addB = C and it should satisfy the inequality A + addA > B + addB.
- Now, since addB = C – addA and put it in the inequality:Â
Â
A + addA > B + (C - addA) or, 2addA > C + B - A or, 2addA >= C + B - A + 1 or, addA >= (C + B - A + 1) / 2
- Since addA must be non negative, addA = max(0, (C + B – A + 1) / 2).
- The division should be ceiling division, thus we can rewrite as addA = max(0, (C + B – A + 2) / 2).
- Let this value be equal to minAddA. Since all integer values addA from [minAddA, C], satisfies the relation A + addA > B + addB, so the required number of ways is equal to max(0, C – minAddA + 1).
Below is the implementation of the above approach:
Â
C++
// C++ implementation of the above approachÂ
#include <bits/stdc++.h>using namespace std;Â
// Function to count the number of ways to divide// C into two parts and add to A and B such// that A is strictly greater than Bint countWays(int A, int B, int C){    // Minimum value added to A to satisfy    // the given relation    int minAddA = max(0, (C + B - A + 2) / 2);Â
    // Number of different values of A, i.e.,    // number of ways to divide C    int count_ways = max(C - minAddA + 1, 0);Â
    return count_ways;}Â
// Driver codeint main(){Â Â Â Â int A = 3, B = 5, C = 5;Â
    cout << countWays(A, B, C);Â
    return 0;} |
Java
// Java implementation of the above approachimport java.util.*;Â
class GFG{Â
    // Function to count the number of ways to divide    // C into two parts and add to A and B such    // that A is strictly greater than B    static int countWays(int A, int B, int C)    {        // Minimum value added to A to satisfy        // the given relation        int minAddA = Math.max(0, (C + B - A + 2) / 2);             // Number of different values of A, i.e.,        // number of ways to divide C        int count_ways = Math.max(C - minAddA + 1, 0);             return count_ways;    }         // Driver code    public static void main(String args[])    {        int A = 3, B = 5, C = 5;             System.out.println(countWays(A, B, C));    }}Â
// This code is contributed by AbhiThakur |
Python3
# Python3 implementation of the above approachÂ
# Function to count the number of ways to divide# C into two parts and add to A and B such# that A is strictly greater than Bdef countWays(A, B, C):         # Minimum value added to A to satisfy    # the given relation    minAddA = max(0, (C + B - A + 2) // 2)         # Number of different values of A, i.e.,    # number of ways to divide C    count_ways = max(C - minAddA + 1, 0)         return count_waysÂ
# Driver codeA = 3B = 5C = 5print(countWays(A, B, C))Â
# This code is contributed by shivanisingh |
C#
// C# implementation of the above approachusing System;Â Â class GFG{Â
// Function to count the number of ways to divide// C into two parts and add to A and B such// that A is strictly greater than Bstatic int countWays(int A, int B, int C){    // Minimum value added to A to satisfy    // the given relation    int minAddA = Math.Max(0, (C + B - A + 2) / 2);Â
    // Number of different values of A, i.e.,    // number of ways to divide C    int count_ways = Math.Max(C - minAddA + 1, 0);Â
    return count_ways;}Â
// Driver Codepublic static void Main(String[] args){Â Â Â Â int A = 3, B = 5, C = 5;Â
    Console.Write(countWays(A, B, C));}Â
}Â
// This code is contributed by shivanisinghss2110 |
Javascript
<script>Â
// Javascript implementation of the above approachÂ
// Function to count the number of ways to divide// C into two parts and add to A and B such// that A is strictly greater than Bfunction countWays(A, B, C){    // Minimum value added to A to satisfy    // the given relation    var minAddA = Math.max(0, parseInt((C + B - A + 2) / 2));Â
    // Number of different values of A, i.e.,    // number of ways to divide C    var count_ways = Math.max(C - minAddA + 1, 0);Â
    return count_ways;}Â
// Driver codevar A = 3, B = 5, C = 5;document.write( countWays(A, B, C));Â
// This code is contributed by rutvik_56.</script> |
Output:Â
2
Â
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!



