Given an integer n
, return a list of all simplified fractions between 0
and 1
(exclusive) such that the denominator is less-than-or-equal-to n
. You can return the answer in any order. A fraction is simplified if the greatest common divisor (GCD) of its numerator and denominator is 1.
For example:
n = 2
, the output should be ["1/2"]
. This is because "1/2" is the only unique fraction with a denominator less-than-or-equal-to 2.n = 3
, the output should be ["1/2","1/3","2/3"]
.n = 4
, the output should be ["1/2","1/3","1/4","2/3","3/4"]
. Note that "2/4" is not a simplified fraction because it can be simplified to "1/2".How would you approach this problem, considering efficiency and the constraints 1 <= n <= 100
?
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding simplified fractions means we're going to try every possible fraction and check if it meets our criteria. We'll generate all fractions within the given range and then determine which ones are in their simplest form.
Here's how the algorithm would work step-by-step:
def simplified_fractions(integer_number):
simplified_fraction_list = []
for numerator in range(1, integer_number + 1):
for denominator in range(1, integer_number + 1):
# Avoid division by zero
if denominator != 0:
# Calculate the greatest common divisor
greatest_common_divisor = calculate_greatest_common_divisor(numerator, denominator)
# Check if the fraction is simplified
if greatest_common_divisor == 1:
#Ensures no duplicates and no fractions > 1, simplifies problem per prompt.
if numerator <= denominator:
simplified_fraction_list.append(f'{numerator}/{denominator}')
return simplified_fraction_list
def calculate_greatest_common_divisor(first_number, second_number):
#Euclid's algorithm to find the GCD, works by recursion
while(second_number):
first_number, second_number = second_number, first_number % second_number
return first_number
The key to solving this problem efficiently is finding the greatest common divisor (GCD) of the numerator and denominator. Once we find the GCD, we can simplify the fraction. By systematically checking all possible fractions, we can collect the simplified results.
Here's how the algorithm would work step-by-step:
def simplified_fractions(maximum_denominator):
simplified_fractions_set = set()
result = []
for numerator in range(1, maximum_denominator + 1):
for denominator in range(numerator + 1, maximum_denominator + 1):
# Use GCD to find the greatest common divisor.
numerator_copy = numerator
denominator_copy = denominator
while denominator_copy:
numerator_copy, denominator_copy = denominator_copy, numerator_copy % denominator_copy
greatest_common_divisor = numerator_copy
simplified_numerator = numerator // greatest_common_divisor
simplified_denominator = denominator // greatest_common_divisor
# Check for duplicates
fraction_string = str(simplified_numerator) + "/" + str(simplified_denominator)
if fraction_string not in simplified_fractions_set:
# Store the fraction string to avoid duplicates
simplified_fractions_set.add(fraction_string)
result.append(fraction_string)
return result
Case | How to Handle |
---|---|
n = 1 | Return an empty list since no fractions are possible with only one number. |
n = 2 (minimum valid input) | The solution should correctly identify and return the single reduced fraction. |
Input includes zero (0 as denominator) | Division by zero should be avoided; skip the fraction if the denominator is 0. |
Duplicate fractions exist before reduction (e.g., 2/4 and 1/2) | Use a set to store the string representation of reduced fractions and prevent duplicates. |
Integer overflow in numerator or denominator during GCD calculation or fraction construction with large n | Use appropriate data types (e.g., long) and check for potential overflow during calculation. |
Negative input values | Handle negative signs appropriately, reducing fractions to their standard form (negative sign on numerator or none if both are negative). |
Large n (performance considerations) | GCD calculation should be optimized for efficiency; the overall complexity will still be at least O(n^2 * log(max_val)), but minimize constant factors. |
All fractions simplify to the same value | The set should correctly handle adding multiple instances of the same simplified fraction string, effectively storing only one. |