# 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 B ` `int` `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 code ` `int` `main() ` `{ ` ` ` `int` `A = 3, B = 5, C = 5; ` ` ` ` ` `cout << countWays(A, B, C); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` `import` `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 ` |

*chevron_right*

*filter_none*

## 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 B ` `def` `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 code ` `A ` `=` `3` `B ` `=` `5` `C ` `=` `5` `print` `(countWays(A, B, C)) ` ` ` `# This code is contributed by shivanisingh ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `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 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; ` ` ` ` ` `Console.Write(countWays(A, B, C)); ` `} ` ` ` `} ` ` ` `// This code is contributed by shivanisinghss2110 ` |

*chevron_right*

*filter_none*

**Output:**

2

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count number of ways to divide a number in 4 parts
- Count the subarray with sum strictly greater than the sum of remaining elements
- Find the number of ways to divide number into four parts such that a = c and b = d
- Number of ways to divide string in sub-strings such to make them in lexicographically increasing sequence
- Divide N into K unique parts such that gcd of those parts is maximum
- Count number of ways to divide an array into two halves with same sum
- Count the number of ways to divide N in k groups incrementally
- Count ways to divide circle using N non-intersecting chord | Set-2
- Count of ways to make Array sum even by removing only one element
- Divide a number into two parts
- Divide N into K parts in the form (X, 2X, ... , KX) for some value of X
- Divide a big number into two parts that differ by k
- Divide number into two parts divisible by given numbers
- Divide a number into two parts such that sum of digits is maximum
- Divide an isosceles triangle in two parts with ratio of areas as n:m
- Divide a sorted array in K parts with sum of difference of max and min minimized in each part
- Minimum cuts required to divide the Circle into equal parts
- Number of ways N can be divided into four parts to construct a rectangle
- Find the count of Strictly decreasing Subarrays
- Minimum removals from array to make GCD greater

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.