Start learning free

LESSON 87 OF 409 · CONDITIONAL STATEMENTS - CHALLENGES

Second Largest Number in O(1) Time and O(1) Space using C++

Practice Read the full explanation

Given 3 integers (the parameters A, B and C; Rust and Go name them in lowercase), return the second largest number.

Example 1:

Input: A = 7, B = 3, C = 9
Output: 7

Example 2:

Input: A = 12, B = 8, C = 3
Output: 8

Note:

Your algorithm should run in O(1) time and use O(1) extra space.


Hints:

There are three candidates to the solution: A, B and C. We should try out each of them until we find the correct one. How can we check if A is the solution? What should happen for A to be the second largest value?

A is the second largest value if the smaller of B and C is less than or equal to A, and A is less than or equal to the larger of B and C. We can write an if statement checking these two conditions and, if they hold, return A. What if the conditions don't hold and A is not the solution? What should we do next?

We should test B. Similarly, B is the second largest value if the smaller of A and C is less than or equal to B, and B is less than or equal to the larger of A and C. We can write a second if statement checking these conditions and, if they hold, return B. What if not even B is the solution?

Then, it's clear that C is the solution, so we should return C. We don't even need to check the conditions anymore.
STEP 1 OF 3
    Ctrl + Enter to check

    C++ · CONDITIONAL STATEMENTS - CHALLENGES

    Second Largest Value in C++, explained

    Understanding the Problem

    The core challenge of this problem is to identify the second largest number among three given integers. This problem is significant in various applications where ranking or ordering of elements is required. A common pitfall is to overcomplicate the solution by using sorting or additional data structures, which are unnecessary given the constraints.

    Approach

    To solve this problem, we need to identify the second largest number among the three given integers. A naive approach would be to sort the numbers and pick the second element, but this is not optimal as it does not meet the O(1) time and space complexity requirements.

    Instead, we can use a series of conditional checks to determine the second largest number directly:

    • Check if A is the second largest by ensuring it is not the smallest or the largest.
    • If A is not the second largest, check B using similar conditions.
    • If neither A nor B is the second largest, then C must be the second largest.

    Algorithm

    Here is a step-by-step breakdown of the algorithm:

    1. Check if A is the second largest by verifying if it lies between the minimum and maximum of B and C.
    2. If A is not the second largest, check B using similar conditions.
    3. If neither A nor B is the second largest, then C is the second largest by default.

    Code Implementation

    
    #include <iostream>
    using namespace std;
    
    // Function to find the second largest number among three integers
    int secondLargest(int A, int B, int C) {
        // Check if A is the second largest
        if ((A >= B && A <= C) || (A >= C && A <= B)) {
            return A;
        }
        // Check if B is the second largest
        if ((B >= A && B <= C) || (B >= C && B <= A)) {
            return B;
        }
        // If neither A nor B is the second largest, then C is the second largest
        return C;
    }
    
    int main() {
        // Test cases
        cout << "Second largest of (7, 3, 9): " << secondLargest(7, 3, 9) << endl; // Output: 7
        cout << "Second largest of (12, 8, 3): " << secondLargest(12, 8, 3) << endl; // Output: 8
        return 0;
    }
    

    Complexity Analysis

    The time complexity of this approach is O(1) because we are using a constant number of comparisons. The space complexity is also O(1) as we are not using any additional data structures.

    Edge Cases

    Potential edge cases include:

    • All three numbers are the same (e.g., A = B = C).
    • Two numbers are the same and one is different (e.g., A = B, A != C).

    These cases are handled by the conditional checks in the algorithm.

    Testing

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

    • Simple cases with distinct numbers.
    • Cases where two or all three numbers are the same.
    • Cases with negative numbers.

    Using a variety of test cases ensures the robustness of the solution.

    Thinking and Problem-Solving Tips

    When approaching such problems, it is essential to:

    • Understand the constraints and requirements.
    • Think about the simplest and most efficient way to solve the problem.
    • Break down the problem into smaller, manageable parts.

    Practicing similar problems and studying algorithms can significantly improve problem-solving skills.

    Conclusion

    In this blog post, we discussed how to find the second largest number among three integers in O(1) time and O(1) space using C++. We covered the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is crucial for developing efficient algorithms and improving problem-solving skills.

    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