LeetCode Problem

How to Solve Simplified Fractions

To solve the "Simplified Fractions" problem, identify all fractions between 0 and 1 where the denominator is less than or equal to n. Ensure that each fraction is fully simplified by checking that the greatest common divisor (GCD) of the numerator and denominator is 1. This problem requires a mix of math and string manipulation to generate the results.

GhostInterview Help

Need help with Simplified Fractions without spending extra time grinding it?

GhostInterview can read Simplified Fractions from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #1447Math plus StringReviewed 2026-03-08
Difficulty
Medium
Primary pattern
Math plus String
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

To solve the "Simplified Fractions" problem, identify all fractions between 0 and 1 where the denominator is less than or equal to n. Ensure that each fraction is fully simplified by checking that the greatest common divisor (GCD) of the numerator and denominator is 1. This problem requires a mix of math and string manipulation to generate the results.

Problem Statement

Given an integer n, return all simplified fractions between 0 and 1 (exclusive) where the denominator is less than or equal to n. Each fraction should be in the form of 'numerator/denominator' and you can return the list in any order.

A fraction is considered simplified if the greatest common divisor (GCD) of its numerator and denominator is 1. For example, '2/4' is not simplified because it can be reduced to '1/2'.

Examples

Example 1

Input: n = 2

Output: ["1/2"]

"1/2" is the only unique fraction with a denominator less-than-or-equal-to 2.

Example 2

Input: n = 3

Output: ["1/2","1/3","2/3"]

Example details omitted.

Example 3

Input: n = 4

Output: ["1/2","1/3","1/4","2/3","3/4"]

"2/4" is not a simplified fraction because it can be simplified to "1/2".

Constraints

  • 1 <= n <= 100

Solution Approach

Loop through possible fractions

For each denominator from 2 to n, iterate through all possible numerators (from 1 to denominator-1). Check if the GCD of the numerator and denominator is 1 to determine if the fraction is simplified.

Use GCD to simplify fractions

To efficiently check if a fraction is simplified, use the greatest common divisor (GCD) of the numerator and denominator. If the GCD is 1, the fraction is simplified.

Generate result as strings

For each valid simplified fraction, convert the numerator and denominator into a string of the form 'numerator/denominator' and append it to the result list.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

Time complexity depends on the number of fractions generated, which is approximately O(n^2) due to the nested loops. For each fraction, checking the GCD takes constant time, and space complexity is also O(n^2) to store the fractions.

What Interviewers Usually Probe

  • Understanding of GCD and its application to fraction simplification
  • Ability to work with loops and basic string formatting
  • Familiarity with time and space complexity analysis in mathematical problems

Common Pitfalls or Variants

Common pitfalls

  • Forgetting to check the GCD of the numerator and denominator to ensure the fraction is simplified
  • Generating fractions in the wrong order or including fractions like '2/4' that aren't simplified
  • Not handling edge cases, such as when n is 1 (which would not result in any simplified fractions)

Follow-up variants

  • Modify the problem to return fractions in ascending order by value
  • Return only fractions where the numerator is prime
  • Optimize the solution by using a sieve approach to identify valid fractions

How GhostInterview Helps

  • GhostInterview provides an efficient way to implement the logic for generating and simplifying fractions with a step-by-step approach.
  • It helps quickly identify edge cases and optimize solutions through practical hints.
  • With GhostInterview, you can simulate the process and improve your understanding of the mathematical patterns behind fraction simplification.

Topic Pages

FAQ

How can I generate all simplified fractions for a given n?

Loop through each denominator from 2 to n and check every possible numerator. Use the GCD to determine if the fraction is simplified.

What is a simplified fraction?

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

What is the time complexity of solving this problem?

The time complexity is approximately O(n^2) due to the nested loops and GCD checks for each fraction.

What is the primary pattern in this problem?

The primary pattern involves math (GCD and fractions) combined with string manipulation to output the result.

How do I handle fractions like '2/4' that are not simplified?

By checking the GCD of the numerator and denominator, you can avoid adding unsimplified fractions to the result.

GhostInterview Solver

Need direct help with Simplified Fractions instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Simplified Fractions from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.