Hand Shakes in Python with O(1) Time Complexity


There are n friends in a group. When they meet, everybody shakes hands with everybody. How many hand shakes happen?

Example:

Input: n = 3

Output: 3

Explanation:
Considering A, B, and C are the 3 friends.
The hand shakes are: (A, B), (A, C) and (B, C)

Understanding the Problem

The core challenge of this problem is to determine the number of unique handshakes that occur when each person in a group of n friends shakes hands with every other person exactly once. This problem is significant in combinatorics and has applications in network theory, social networking, and more.

Potential pitfalls include misunderstanding that each handshake is unique and not double-counting pairs.

Approach

To solve this problem, we need to count the number of unique pairs that can be formed from n friends. This is a classic combinatorial problem where we need to choose 2 people out of n, which can be represented mathematically as "n choose 2" or C(n, 2).

The formula for combinations is:

C(n, 2) = n! / (2! * (n - 2)!)

Which simplifies to:

C(n, 2) = n * (n - 1) / 2

This formula gives us the number of unique handshakes.

Algorithm

1. Read the input value n.

2. Use the formula n * (n - 1) / 2 to calculate the number of handshakes.

3. Return the result.

Code Implementation

def count_handshakes(n):
    # Using the formula n * (n - 1) / 2 to calculate the number of handshakes
    return (n * (n - 1)) // 2

# Example usage
n = 3
print(count_handshakes(n))  # Output: 3

Complexity Analysis

The time complexity of this solution is O(1) because the calculation involves a constant number of operations regardless of the input size. The space complexity is also O(1) as we are using a constant amount of space.

Edge Cases

1. n = 0: No friends, so no handshakes. The output should be 0.

2. n = 1: One friend, so no handshakes. The output should be 0.

3. n = 2: Two friends, one handshake. The output should be 1.

Testing

To test the solution comprehensively, consider the following test cases:

assert count_handshakes(0) == 0
assert count_handshakes(1) == 0
assert count_handshakes(2) == 1
assert count_handshakes(3) == 3
assert count_handshakes(4) == 6
assert count_handshakes(5) == 10

These test cases cover the edge cases and typical scenarios.

Thinking and Problem-Solving Tips

When approaching combinatorial problems, it's helpful to break down the problem into smaller parts and look for patterns. Practice similar problems to improve your problem-solving skills and understand the underlying principles of combinatorics.

Conclusion

In this blog post, we discussed how to solve the problem of counting handshakes in a group of n friends using a combinatorial approach. We derived the formula, implemented the solution in Python, and analyzed its complexity. Understanding such problems is crucial for developing strong problem-solving skills in computer science.

Additional Resources

For further reading and practice, consider the following resources: