Simplified Fractions - Problem

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.

A fraction is simplified if the greatest common divisor (GCD) of the numerator and denominator is 1.

You can return the answer in any order.

Input & Output

Example 1 — Basic Case
$ Input: n = 2
Output: ["1/2"]
💡 Note: Only one fraction with denominator ≤ 2: 1/2 is already simplified since gcd(1,2) = 1
Example 2 — Multiple Denominators
$ Input: n = 3
Output: ["1/2", "1/3", "2/3"]
💡 Note: Denominators 2 and 3: 1/2 (gcd=1), 1/3 (gcd=1), 2/3 (gcd=1). All are simplified.
Example 3 — With Non-Simplified
$ Input: n = 4
Output: ["1/2", "1/3", "1/4", "2/3", "3/4"]
💡 Note: Skip 2/4 because gcd(2,4) = 2, so it simplifies to 1/2 which we already have.

Constraints

  • 1 ≤ n ≤ 100

Visualization

Tap to expand
Simplified Fractions INPUT n = 2 (max denominator) Fractions to Check: 1/2 2/2 Range: 0 < fraction < 1 0 1/2 1 Input Value: n = 2 ALGORITHM STEPS 1 Iterate Denominators d = 2 to n (skip d=1) 2 Iterate Numerators num = 1 to d-1 3 Check GCD If GCD(num, d) = 1 4 Add to Result Fraction is simplified Euler's Totient: phi(n) d=2: check num=1 GCD(1, 2) = 1 OK GCD(2, 2) = 2 SKIP phi(2) = 1 fraction FINAL RESULT Simplified Fractions Found: 1/2 GCD(1,2) = 1 [OK] Output Array: ["1/2"] Total: 1 simplified fraction for denominators 1 to 2 Key Insight: Euler's Totient phi(n) counts integers from 1 to n that are coprime to n (GCD = 1). For each denominator d, phi(d) gives the count of valid numerators, ensuring O(n*log(n)) efficiency. A fraction num/d is simplified when GCD(num, d) = 1, meaning they share no common factors. TutorialsPoint - Simplified Fractions | Euler's Totient Function Approach
Asked in
Google 15 Microsoft 12
28.5K Views
Medium Frequency
~15 min Avg. Time
834 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen