Codeforces Three Brothers Problem in Python and C++

Published September 03, 2024

If you are preparing for USACO, ACSL or any other competitive programming, Codeforces is a good place to practice your programing. This article contains my Python and C++ solutions for the Three Brothers problem 2010B, a part of Codeforces Testing Round 19 (Div. 3).

Codeforces Three Brothers problem

Three Brothers problem

Please read the Three Brothers problem on Codeforces first. Make sure you understand the problem fully. Write the algorithm on paper before coding anything. You can also draw a flowchart instead.

Programming Solution Logic

There is only one line of input, and that is two numbers a and b separated by a single space.

The output should be a single integer which is the missing number.

The values of a, b and the output will be 1, 2, 3, in any order. The goal is to accept any of these two numbers as input and return the third number as output.

We can do this in multiple ways. Let us look at two algorithms.

Algorithm 1: Using array / list

  1. Accept values of a and b and store them in an array / list.
  2. Find and print the difference between the predefined array with items 1, 2, 3. That is our answer.

Algorithm 2: Subtracting values of a and b from 6

  1. We know that 1 + 2 + 3 = 6.
  2. If we have one number x missing from the list of 1, 2, 3, and the other two are a and b, we can always find the value of x with the expression 6 - (a + b).
    This is how we do it mathematically:
    a + b + x = 6

=> x = 6 - (a + b)

Test Cases

Test Case 1:

Input

3 1

Output

2

Test Case 2:

Input

3 2

Output

1

Install Python or C++

If you do not have Python or C++, you should first install it. Codeforces uses Python 3.8 for evaluating your code. If you are using C++, they have versions 17, 20 and 23.

Python Solution #1

three_brothers.py

# https://codeforces.com/problemset/problem/1985/B

# line 1 : a b

print(*set('1 2 3') - set(input()))

This is a one-line solution. We capture the input of two numbers a and b using input() and create a set using those two numbers. A set may not maintain the order of the numbers, but we don't care about the order.

set(input())

We create a predefined set with 1, 2, 3.

set('1 2 3')

Set difference will result in a set with the missing numbers. To print just the first item in the set, we prepend it with an asterisk.

print(*set('1 2 3') - set(input()))

That's it!

Python Solution #2

three_brothers_2.py

print(6 - sum([int(i) for i in input().split()]))

This is also a one-line solution. We capture the input of two numbers a and b using input(), split them with split(), convert them to int() and apply sum() over the list. This essentially finds the value of a + b. Then we subtract this number from 6 to get the answer.

Running the Python code

Run the Python code from your Terminal. I am using macOS, but you can use any Linux-based OS as well.

Enter this as input and press ENTER.

3 2

Output:

1

My C++ solution

three_brothers.cpp

This is my C++ solution:

#include <iostream>

using namespace std;

// Main function
int main() {
    int a, b;
    cin >> a >> b;
    cout << 6 - a - b;
}

In the C++ code, we used the logic of the second algorithm. This is far simpler than the first algorithm, because we don't have to use vectors, so we'll end the C++ version here.

Running the C++ code

Compile the C++ code from your Terminal. This command creates a.out.

$ g++ three_brothers.cpp

Now, run the test cases against a.out.

3 2

Output:

1

Submit your code

Submit your Python code and C++ code to Codeforces. When your code successfully passes all test cases, you will get a green "Accepted" message with the time taken.

Which is faster?

In the Codeforces submission page, these were the results:

Python 3                    -- Time taken: 62 ms
C++20 (GCC 13-64)           -- Time taken: 46 ms
C++23 (GCC 14-64, msys2)    -- Time taken: 46 ms

The C++ program was 16 ms faster. But this may vary depending on how busy the server was at the time of submission.

What about software design, conventions and standards?

This is competitive programming. The goal is to complete the program with minimum effort and maximum efficiency. Using single character variables and ugly formatted code are okay, as long as the program runs and runs well within the scope. That is fine, as long as your program works and you are able to submit it.

Later in life, when you start working in a professional environment, please beautify your ugly code, follow conventions, add comments and make your code readable. Readability and maintainability is key if you want to be a successful software engineer.

Conclusion

If there is anything you would like me to add to this article, feel free to comment below or contact me. Thanks.

Related Posts

If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.

Disclaimer: Our website is supported by our users. We sometimes earn affiliate links when you click through the affiliate links on our website.

Last Updated: September 03, 2024.     This post was originally written on September 03, 2024.