Start learning free

LESSON 79 OF 410 · CONDITIONAL STATEMENTS

Parity of Number in Python

Practice Read the full explanation

Given a non-negative integer n, check its parity and return "even" or "odd"

A number is "even" if it is divisible by 2 and "odd" otherwise

Example:

Input: n = 12

Output: "even"

Explanation:
12 is divisible by 2, so it is even
Hints:

You can make use of modulo operator with an if statement

12 % 2 is 0, therefore 12 is "even"
13 % 2 is 1, therefore 13 is "odd"
STEP 1 OF 3
    Ctrl + Enter to check

    PYTHON · CONDITIONAL STATEMENTS

    Parity Of Number in Python, explained

    Understanding the Problem

    The core challenge of this problem is to determine whether a given non-negative integer n is even or odd. This is a fundamental problem in computer science and mathematics with applications in various fields such as algorithm design, data structures, and more.

    Common applications include determining the parity of elements in an array, optimizing algorithms based on even or odd indices, and more.

    Potential pitfalls include misunderstanding the modulo operation or incorrectly handling edge cases such as zero.

    Approach

    To solve this problem, we can use the modulo operator. The modulo operator (%) returns the remainder of a division operation. If a number n is divisible by 2 (i.e., n % 2 == 0), then it is even; otherwise, it is odd.

    Naive Solution

    A naive solution would involve checking the divisibility of the number by 2 using the modulo operator. This approach is straightforward and efficient for this problem.

    Optimized Solution

    The naive solution is already optimal for this problem. The modulo operation and comparison both run in constant time, O(1).

    Algorithm

    1. Take the input number n.
    2. Check if n % 2 == 0.
    3. If true, return "even".
    4. Otherwise, return "odd".

    Code Implementation

    def check_parity(n):
        """
        Function to check the parity of a number.
        
        Parameters:
        n (int): A non-negative integer
        
        Returns:
        str: "even" if n is even, "odd" if n is odd
        """
        # Check if the number is divisible by 2
        if n % 2 == 0:
            return "even"
        else:
            return "odd"
    
    # Example usage
    n = 12
    print(check_parity(n))  # Output: "even"
    

    Complexity Analysis

    The time complexity of this solution is O(1) because the modulo operation and the comparison both take constant time.

    The space complexity is also O(1) as we are not using any additional space that scales with the input size.

    Edge Cases

    Potential edge cases include:

    • n = 0: The function should return "even" as 0 is divisible by 2.
    • Very large values of n: The function should handle large integers efficiently due to Python's support for arbitrary-precision integers.

    Example edge case:

    Input: n = 0
    Output: "even"

    Testing

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

    • Simple cases: n = 1, n = 2
    • Edge cases: n = 0, very large n
    • Random cases: n = 15, n = 28

    Example test cases:

    assert check_parity(1) == "odd"
    assert check_parity(2) == "even"
    assert check_parity(0) == "even"
    assert check_parity(15) == "odd"
    assert check_parity(28) == "even"

    Thinking and Problem-Solving Tips

    When approaching such problems, consider the following tips:

    • Understand the problem requirements and constraints thoroughly.
    • Think about the simplest and most efficient way to solve the problem.
    • Consider edge cases and how your solution handles them.
    • Practice similar problems to improve your problem-solving skills.

    Conclusion

    In this blog post, we discussed how to determine the parity of a number using a simple and efficient approach. We covered the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is crucial for developing strong problem-solving skills in computer science.

    We encourage you to practice and explore further to enhance your understanding and skills.

    Additional Resources

    Keep going with PythonAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free