You are given an integer ‘N’. Your task is to find all “simplified fractions” in the range [0, 1] whose denominator is less than or equal to ‘N’.
Note:
1. A “fraction” is not a whole number. It is a part of a whole.
2. When the numerator and denominator of a fraction are no longer to be reduced to any smaller number upon division by any common integer, the fraction is known as a “simplified fraction”.
3. For example, 1/2 is a simplified fraction but 2/4 is not a simplified fraction as we can reduce 2/4 to 1/2.
The first line contains an integer ‘T’, which denotes the number of test cases to be run. Then, the T test cases follow.
The first and only line of each test case contains an integer, ‘N’, as described in the problem statement.
Output Format:
For each test case, print the “list of strings representing all simplified fractions”, as described in the problem statement. If there are no such fractions, then print ‘-1’ without any quotes.
Output for each test case will be printed in a separate line.
Note:
1. You do not need to print anything. It has already been taken care of. Just implement the given function.
2. You can return the list in “any order”.
1 <= ‘T’ <= 10
1 <= ‘N’ <= 100
Time Limit: 1 sec
1
3
1/2 1/3 2/3
The only fractions 1/2, 1/3, and 2/3 satisfied all the conditions. ‘1/4’ is not a valid fraction in this case because the denominator must be less than or equal to the ‘3’.
1
1
-1
There are no valid fractions available whose denominator is less than or equal to ‘1’.
If the GCD of numerator and denominator is 1, then the fraction is simplified.
Finally, return the 'ANS'.
O((N^2)*(logN)), where ‘N’ is an integer given in the problem.
We are forming all fractions by using a nested for loop, and for each fraction, we are calculating the GCD, which will takes O(logN) time. So, the overall time complexity will be O(N^2*(logN)).
O(N^2), where ‘N’ is an integer given in the problem.
As we are using a list to store the valid fractions. The size of the list is always to be less than N^2. Hence, the space complexity will be O(N^2).