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


Given 3 integers, 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.


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:

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:

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

Testing

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

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:

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: