Start learning free

LESSON 74 OF 409 · CONDITIONAL STATEMENTS

Parity of Number in C++

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

    C++ · CONDITIONAL STATEMENTS

    Parity Of Number in C++, 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 properties, and more.

    Potential pitfalls include misunderstanding the modulo operation or not handling edge cases like zero correctly.

    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 last digit of the number, but this is not optimal and can be cumbersome for large numbers.

    Optimized Solution

    The optimized solution involves using the modulo operator, which is both simple and efficient. This approach has a constant time complexity of O(1) because it involves a single arithmetic operation.

    Algorithm

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

    Code Implementation

    #include <iostream>
    #include <string>
    
    // Function to check the parity of a number
    std::string checkParity(int n) {
        // If n is divisible by 2, it is even
        if (n % 2 == 0) {
            return "even";
        } else {
            // Otherwise, it is odd
            return "odd";
        }
    }
    
    int main() {
        int n;
        std::cout << "Enter a number: ";
        std::cin >> n;
        std::cout << "The number " << n << " is " << checkParity(n) << "." << std::endl;
        return 0;
    }
    

    Complexity Analysis

    The time complexity of this solution is O(1) because it involves a single modulo operation, which is a constant-time operation. 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 output should be "even" because 0 is divisible by 2.
    • Very large values of n: The algorithm should handle large integers efficiently.

    Testing

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

    • n = 0: Expected output is "even".
    • n = 1: Expected output is "odd".
    • n = 2: Expected output is "even".
    • n = 999999: Expected output is "odd".
    • n = 1000000: Expected output is "even".

    Thinking and Problem-Solving Tips

    When approaching such problems, it is essential to:

    • Understand the problem statement clearly.
    • Think about the simplest and most efficient way to solve the problem.
    • Consider edge cases and test your solution against them.
    • Practice similar problems to improve problem-solving skills.

    Conclusion

    In this blog post, we discussed how to determine the parity of a number using a simple and efficient algorithm. 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.

    Additional Resources

    For further reading and practice, consider the following resources:

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