Last Updated: 2 Feb, 2022

Ninja And The Apples

Easy
Asked in company
NI (National Instruments)

Problem statement

Ninja has been given two baskets of apples containing ‘A’ and ‘B’ apples each. Also, he has been given extra ‘C’ apples separately.

This apple will be gifted to the Ninja’s best buddies; Ninja wants to do some partiality so that the number of apples in the first basket is strictly greater than the number of apples in the second basket.

The ninja can do it using extra ‘C’ apples, he must have to use all the extra apples and transfer them in both the first and second basket.

Your task is to help Ninja by calculating a number of possible arrangements of apples so that the resultant configuration is feasible for Ninja to do the partiality.

EXAMPLE:
Input: 'A' = 5, 'B' = 3, 'C' = 4

Output: 3

Here only three possible combinations are:

(7, 5) Transfer 2 apples to the first basket and 2 apples to the second basket.
(9, 3) Transfer 4 apples to the first basket and 0 apples to the second basket.
(8, 4) Transfer 3 apples to the first basket and 1 apple to the second basket.

Only above combinations for (A, B) satisfies the condition that all 'C'  apples are used and ('A' > 'B')
Input Format :
The first line will contain integer 'T' denoting the number of test cases. For each test case, there will be a single line containing three integers 'A', 'B', and 'C' only.
Output format :
For each test case, print a single line containing a single integer number of possible arrangements of apples so that the resultant configuration is feasible for Ninja to do the partiality.
Note :
You don't need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= 'T' <= 10^5
0 <= 'A', 'B', 'C'<= 10^8

Time Limit : 1 sec

Approaches

01 Approach

Approach: 

 

  • Say 'U' and 'V' be the number of apples transferred to the first and second basket respectively.
  • 'A' + 'U' > 'B' + 'V' must be satisfied.
  • 'A' + 'U' > 'B' + ('C' - 'U')
  • 'U' > ('C' - 'A' + 'B')/2
  • Since 'U' can't be negative
  • 'U' > max(0, ('C' - 'A' + 'B')/2)
  • Since 'U' > max(0, ('C' - 'A' + 'B')/2) we can assume 'U' = max(0, ('C' - 'A' + 'B')/2 + 1).
  • Let 'P' will be the minimum possible value of 'U'. 'P' = 'max(0, ('C' - 'A' + 'B' + 2)/2)'.
  • Return 'max(0, 'C' - 'P' + 1).

 

Algorithm :  
 

  1. Initialize the integer 'P' = 'max(0, ('C' - 'A' + 'B' + 2)/2)'
  2. Return 'max(0, 'C' - 'P' + 1).