How to find pairs with product greater than sum
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Finding pairs of numbers where the product is greater than the sum can be an intriguing computational problem that involves both elementary algebra and coding ability. Such pairs are significant in certain problem-solving scenarios, algorithm optimization tasks, and even competitive programming. In this article, we'll explore the conditions under which two numbers yield a product greater than their sum, provide a mathematical rationale, and demonstrate how to implement this in code.
Understanding the Problem
Given two numbers, `a` and `b`, we want to determine when:
Rearranging this inequality gives:
Adding 1 to both sides yields:
This expression can be factored as:
This final inequality gives us a clear condition: the product of `(a-1)` and `(b-1)` must be greater than 1.
Mathematical Evaluation
Analyzing the derived inequality `(a-1)(b-1) > 1`, consider both `a` and `b`:
- Case 1: Both `a` and `b` are greater than 2:
If `a > 2` and `b > 2`, then `(a-1) > 1` and `(b-1) > 1`. Thus, the product `(a-1)(b-1)` is naturally greater than 1. - Case 2: One of the numbers is 2 or less:
If one of the numbers is less or equal to 2, the inequality may or may not hold. Special attention should be given when `a` or `b` equals 1, as their corresponding product with the other number frequently fails the inequality, unless both numbers are 1, in which case the inequality holds because: `(1-1)(1-1) = 0 > 1` is false, but for larger numbers, calculations must be explicitly checked. - Case 3: Both numbers are negative:
Negative numbers introduce another complexity. However, negative-numbers scenarios usually result in a negative result for `(a-1)(b-1)`, which won't satisfy the inequality.
Example Cases
- Example 1: `a = 3, b = 4`Here, the condition holds.
- Example 2: `a = 2, b = 5`Here, the condition holds.
- Example 3: `a = -2, b = -3`, which is positive, yet we revisit as inputs due to context validity in the negative range.
Algorithm Implementation
Let's write a Python function that takes a list of numbers and returns all pairs that meet our product > sum condition.
Related reading
- How to find patterns lines, circles,... from a list of points?
- How to find pythagorean triplets in an array faster than ON2?
- How to find repeating sequence of characters in a given array?
- How to find shortest path in dynamic situation
- How to find probability distribution and parameters for real data?
- How to find the closest point on a right rectangular prism 3d rectangle
- How to find smallest substring which contains all characters from a given string?
- How to find Strongly Connected Components in a Graph?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.