beginnerNumber ProblemsJava

Find the Largest of Three Numbers

Return the largest value among three integers using comparisons.

Quick Answer

Start by assuming the first number is the largest, then compare it with the second and third, updating your maximum whenever you find a bigger value. Return the maximum at the end. For 3, 7 and 5 the largest is 7.

Problem Statement

Given three integers a, b, and c, return the largest of the three. If two or more share the maximum value, that shared value is the answer.

For example, among 3, 7, and 5, the largest is 7. Among 10, 10, and 2, the largest is 10.

Input: Three integers a, b, and c.

Output: A single integer: the largest of the three.

Examples

Example 1
Input:  a = 3, b = 7, c = 5
Output: 7

7 is greater than both 3 and 5.

Example 2
Input:  a = 10, b = 10, c = 2
Output: 10

Two values tie at 10, which is the maximum.

Constraints

  • a, b, and c fit in a 32-bit int and may be negative
  • Ties resolve to the shared maximum value

Think Before You Code

Reveal the questions to ask yourself first
  • What is a safe initial guess for the maximum before you compare anything?
  • How many comparisons do you actually need for three values?
  • How should ties be handled so they do not break the logic?

Hints

Open them one at a time — try after each before revealing the next.

Hint 1
Assume the first number is the largest to start with.
Hint 2
Compare that running maximum against each remaining number in turn.
Hint 3
Update the maximum only when a strictly larger value appears; ties need no update.

Approach

Reveal the step-by-step approach

Track a running maximum.

  1. Set max = a.
  2. If b > max, set max = b.
  3. If c > max, set max = c.
  4. Return max.

Two comparisons are enough for three numbers. Using > (not >=) means ties leave max unchanged, which still yields the correct shared value.

Dry Run

Walk through the example step by step

Finding the largest of a = 3, b = 7, c = 5:

step           | max
---------------+-----
max = a        | 3
b > max? 7 > 3 | 7
c > max? 5 > 7 | 7  (no change)
result         | 7

Solution

Reveal the full Java solution
public class LargestOfThree {
    public static int largest(int a, int b, int c) {
        int max = a;
        if (b > max) {
            max = b;
        }
        if (c > max) {
            max = c;
        }
        return max;
    }

    public static void main(String[] args) {
        System.out.println(largest(3, 7, 5));   // 7
        System.out.println(largest(10, 10, 2)); // 10
    }
}

Seeding max with the first value means the two remaining comparisons only ever need to raise it. This "assume then improve" pattern generalises directly to any number of inputs, and because ties use > rather than >=, equal maximums are handled without special cases.

Time: O(1)Space: O(1)

Common Mistakes

  • Comparing only two of the three numbers and forgetting the third.
  • Writing tangled nested if-else chains that miss a case or mishandle ties.

Edge Cases to Test

  • All three equal, e.g. 4, 4, 4, correctly returns 4.
  • Negative numbers, e.g. -3, -7, -1, correctly returns -1.

Interview Follow-Ups

  • How would you return the largest of an arbitrary array of numbers?
  • How could you use Math.max nested calls to write this as a single expression?

Practising for Java interviews?

CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.

Explore the Java Full Stack program →
Chat with us