In programming, especially in competitive coding and technical interviews, problems that require comparisons between values often come up. One such problem is the “Greater of Lesser,” in Java which can be understood as finding the greater value among two numbers unless both are lesser than a specified threshold. This blog will walk you through the problem, the logic to solve it, and a Java implementation, along with a dry run to help you grasp the concept better.
Understanding the “Greater of Lesser” Problem
The “Greater of Lesser” problem is a straightforward yet vital problem that tests your understanding of conditionals, comparisons, and function usage in Java. The challenge typically revolves around determining which number is greater between two provided numbers, considering a specific threshold value. If both numbers are below the threshold, the output should indicate that both are lesser.
Also Read: Count Inversions in an Array | Coding Interview Question | Count Inversions Merge Sort
Problem Statement
Given three integers, a
, b
, and threshold
, your task is to:
- Return the greater of the two numbers
a
andb
. - If both
a
andb
are lesser than thethreshold
, return a specific indicator (such as -1 or a message like “Both lesser”).
Key Concepts and Considerations
- Conditional Statements: You’ll need to use
if-else
or conditional operators to compare the numbers. - Threshold Condition: Ensure that both numbers are compared to the threshold before returning a value.
- Function-Based Approach: We’ll encapsulate the logic within a function to enhance reusability and readability.
Also Read: Exploring the Contrast: Frontend vs. Backend in Application Development

Function-Based Java Implementation
Java Code Implementation
Here’s how you can implement the “Greater of Lesser” problem using a function-based approach in Java:
public class GreaterOfLesser {
// Function to determine the greater number or check if both are lesser than the threshold
public static String greaterOfLesser(int a, int b, int threshold) {
// Check if both numbers are below the threshold
if (a < threshold && b < threshold) {
return "Both lesser";
}
// Compare and return the greater number
if (a > b) {
return "Greater number is: " + a;
} else {
return "Greater number is: " + b;
}
}
// Main method to test the function
public static void main(String[] args) {
int a = 15;
int b = 25;
int threshold = 20;
// Call the function and print the result
System.out.println(greaterOfLesser(a, b, threshold));
}
}
Explanation of the Code
- Function Definition:
- We define a static function
greaterOfLesser
that takes three parameters: two integersa
andb
and an integerthreshold
.
- Threshold Check:
- The function first checks if both
a
andb
are less than the threshold. If they are, the function returns the string “Both lesser.”
- Comparison Logic:
- If the threshold condition isn’t met, the function proceeds to compare
a
andb
. The greater value is returned as a formatted string.
- Main Method:
- The
main
method initializes some test values fora
,b
, andthreshold
and calls thegreaterOfLesser
function, printing the result to the console.
Optimizing the Code
To further optimize the code:
- We can handle edge cases where
a
equalsb
. - We can refactor the function to reduce the number of comparisons when
a
andb
are above the threshold.
Here’s an optimized version:
public class GreaterOfLesser {
// Optimized function to determine the greater number or check if both are lesser than the threshold
public static String greaterOfLesser(int a, int b, int threshold) {
// Single return statement to handle all conditions
if (a < threshold && b < threshold) {
return "Both lesser";
} else if (a >= threshold && (a > b || b < threshold)) {
return "Greater number is: " + a;
} else {
return "Greater number is: " + b;
}
}
// Main method to test the function
public static void main(String[] args) {
int a = 15;
int b = 25;
int threshold = 20;
// Call the function and print the result
System.out.println(greaterOfLesser(a, b, threshold));
}
}
Explanation of the Optimized Code
- Reduced Comparisons: The optimized function reduces unnecessary comparisons by combining conditions.
- Edge Case Handling: It handles edge cases like
a
equalsb
more gracefully, ensuring that only relevant comparisons are made.
Also Read: Understanding Queue: A Detailed Exploration
Dry Run of the Code
Let’s dry run the original code with the values a = 15
, b = 25
, and threshold = 20
to see how it works:
- Initial Values:
a = 15
b = 25
threshold = 20
- Threshold Check:
- The condition
a < threshold && b < threshold
is evaluated as15 < 20 && 25 < 20
, which returnsfalse
becauseb
is not less than the threshold.
- Comparison Logic:
- The code checks if
a > b
, i.e.,15 > 25
, which isfalse
. - Since
a
is not greater thanb
, the code returnsGreater number is: 25
.

- Output:
- The final output is
Greater number is: 25
.
Edge Case Dry Run
Consider the edge case where both a
and b
are below the threshold, e.g., a = 10
, b = 15
, and threshold = 20
.
- The threshold check
10 < 20 && 15 < 20
returnstrue
. - Thus, the function directly returns
Both lesser
.
Also Read: Stack and its Basic Operations
Conclusion
The “Greater of Lesser” problem in Java is a fundamental exercise that hones your understanding of conditionals and function-based problem solving. By encapsulating the logic within a well-structured function, you make your code more modular and easier to debug. This approach is particularly beneficial for coding interviews, where clarity and optimization are paramount.
Ensure that you understand the dry run process, as it can help you trace errors and refine your logic during interviews. Practice implementing this function with various inputs to be fully prepared for any twists the interviewer might throw at you.

FAQs
What is the “Greater of Lesser” problem in Java?
The “Greater of Lesser” problem involves comparing two numbers and a threshold. The goal is to return the greater number unless both are below the threshold, in which case a specific indicator is returned.
How does the threshold affect the comparison?
The threshold serves as a benchmark. If both numbers are less than the threshold, the function returns an indicator rather than the greater of the two numbers.
What if both numbers are equal in the “Greater of Lesser” problem?
If both numbers are equal and either equals or exceeds the threshold, the function will return the string indicating that number as the greater value.
How can I optimize the “Greater of Lesser” function in Java?
To optimize the function, reduce redundant comparisons and handle edge cases, such as when the two numbers are equal or both are below the threshold.
Why use a function-based approach for this problem?
A function-based approach promotes reusability and clarity. It makes the code easier to maintain, test, and extend for future modifications or enhancements.
How can I use this “Greater of Lesser” problem to prepare for coding interviews?
This problem is excellent for practicing conditional logic and comparisons in Java. It also helps you refine your function design skills, which are crucial in coding interviews.
Read More: Copy Array | Copying Array and its Elements | Java Programming | Code with Kamlesh