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


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, but given the constraints, a simple and efficient approach is necessary.

Approach

To solve this problem, we need to determine which of the three numbers is the second largest. We can achieve this by comparing the numbers directly. Here’s a step-by-step approach:

Naive Solution

A naive solution might involve sorting the three numbers and then picking the second element. However, sorting has a time complexity of O(n log n), which is not optimal for this problem.

Optimized Solution

We can solve this problem in constant time O(1) by using a series of conditional checks:

  • Check if A is the second largest by ensuring it is between the minimum and maximum of B and C.
  • If A is not the second largest, check if B is the second largest by ensuring it is between the minimum and maximum of A and C.
  • 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 if B is the second largest by verifying if it lies between the minimum and maximum of A and C.
  3. If neither A nor B is the second largest, return C as the second largest.

Code Implementation

public class SecondLargestNumber {
    public static int findSecondLargest(int A, int B, int C) {
        // Check if A is the second largest
        if (A >= Math.min(B, C) && A <= Math.max(B, C)) {
            return A;
        }
        // Check if B is the second largest
        if (B >= Math.min(A, C) && B <= Math.max(A, C)) {
            return B;
        }
        // If neither A nor B is the second largest, then C must be the second largest
        return C;
    }

    public static void main(String[] args) {
        // Test cases
        System.out.println(findSecondLargest(7, 3, 9)); // Output: 7
        System.out.println(findSecondLargest(12, 8, 3)); // Output: 8
    }
}

Complexity Analysis

The time complexity of this approach is O(1) because we are performing a constant number of operations regardless of the input size. 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 = 5). The function should still return one of the numbers as the second largest.
  • Two numbers are the same and one is different (e.g., A = B = 5, C = 10). The function should correctly identify the second largest.

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.

Example test cases:

System.out.println(findSecondLargest(7, 3, 9)); // Output: 7
System.out.println(findSecondLargest(12, 8, 3)); // Output: 8
System.out.println(findSecondLargest(5, 5, 5)); // Output: 5
System.out.println(findSecondLargest(5, 5, 10)); // Output: 5
System.out.println(findSecondLargest(-1, -2, -3)); // Output: -2

Thinking and Problem-Solving Tips

When approaching such problems, consider the following tips:

  • Break down the problem into smaller, manageable parts.
  • Think about the constraints and how they can simplify the solution.
  • Practice similar problems to improve problem-solving skills.

Conclusion

Understanding and solving problems like finding the second largest number among three integers is crucial for developing strong problem-solving skills. By practicing and exploring different approaches, you can improve your ability to tackle similar challenges efficiently.

Additional Resources

For further reading and practice, consider the following resources:

  • GeeksforGeeks - A comprehensive resource for learning algorithms and data structures.
  • LeetCode - A platform for practicing coding problems and improving problem-solving skills.
  • Coursera - Online courses on algorithms and data structures.