Codeforces 1335A Candies and Two Sisters Solution & Explanation

Difficulty : 800

Problem Description

There are two sisters Alice and Betty. You have n candies. You want to distribute these n candies between two sisters in such a way that:

  • Alice will get a (a>0) candies;
  • Betty will get b (b>0) candies;
  • each sister will get some integer number of candies;
  • Alice will get a greater amount of candies than Betty (i.e. a>b);
  • all the candies will be given to one of two sisters (i.e. a+b=n).

Your task is to calculate the number of ways to distribute exactly n candies between sisters in a way described above. Candies are indistinguishable.

Formally, find the number of ways to represent n as the sum of n=a+b, where a and b are positive integers and a>b.

You have to answer independent test cases.

Input

The first line of the input contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.

The only line of a test case contains one integer n (1≤n≤2⋅109) — the number of candies you have.

Output

For each test case, print the answer — the number of ways to distribute exactly n candies between two sisters in a way described in the problem statement. If there is no way to satisfy all the conditions, print 0.

Examples

Input6
7
1
2
3
2000000000
763243547
Output3
0
0
1
999999999
381621773

Note

For the test case of the example, the 3 possible ways to distribute candies are:

  • a=6, b=1;
  • a=5, b=2;
  • a=4, b=3.

Solution

According to the problem statement, the number of candies Betty can receive, must satisfy the following conditions:

  • b > 0, because Betty must receive at least one candy.
  • a > b, indicating that Alice must receive more candies than Betty.
  • a + b = n, meaning that all candies must be distributed.

Therefore, the range of candies Betty can receive, b, is from 1 to n/2 (excluding n/2).

Since Alice needs to receive more candies than Betty, the minimum number of candies that can be distributed should be at least 3 (Alice gets 2, Betty gets 1).

C# Solution

Solution1

int n = int.Parse(Console.ReadLine());

for(int i=0; i<n; i++){
    int tmp = int.Parse(Console.ReadLine());
    
    if(tmp<3){
        Console.WriteLine(0);
    }
    else{
        Console.WriteLine(tmp%2==0 ? tmp/2-1 : tmp/2);
    }
}

If tmp is an even number, (tmp / 2) – 1 ensures that Alice receives more candies than Betty. This is because in the case of an even number, dividing by 2 results in rounding down, and we want to make sure Alice gets one more candy than Betty, hence the additional subtraction of 1.

On the other hand, if tmp is an odd number, (tmp / 2) automatically rounds down to the nearest integer, ensuring that Alice receives one more candy than Betty without the need for additional subtraction.

Java Solution

Solution1

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.nextLine();

        for (int i = 0; i < n; i++) 
        {
            int tmp = scanner.nextInt();
            scanner.nextLine();
            
            if(tmp<3){
                System.out.println(0);
            }
            else{
                System.out.println(tmp%2==0 ? tmp/2-1 : tmp/2);
            }
        }
    }
}

Conclusion

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts, thanks you~~

✅If you got any problem about the explanation or you need other programming language solution, please feel free to let me know !!

The problem link : Problem – 1335A – Codeforces

Random Codeforces Posts

Leave a Reply

Your email address will not be published. Required fields are marked *