Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Factorial Numbers Not Greater Than N

Easy
0/40
Average time to solve is 20m
profile
Contributed by
242 upvotes
Asked in company
Coinswitch Kuber

Problem statement

You are given an integer ’n’.


Your task is to return a sorted array (in increasing order) containing all the factorial numbers which are less than or equal to ‘n’.


The factorial number is a factorial of a positive integer, like 24 is a factorial number, as it is a factorial of 4.


Note:
In the output, you will see the array returned by you.
Example:
Input: ‘n’ = 7

Output: 1 2 6

Explanation: Factorial numbers less than or equal to ‘7’ are ‘1’, ‘2’, and ‘6’.
Detailed explanation ( Input/output format, Notes, Images )
Input Format
The first line contains one integer, ‘n’, denoting the integer.
Output format:
Return the array as described above.
Note:-
You don't need to print anything. Just implement the given function.
Sample Input 1:
7
Sample Output 1 :
1 2 6
Explanation Of Sample Input 1:
Input: ‘n’ = 7

Output: 1 2 6

Explanation: Factorial numbers less than or equal to ‘7’ are ‘1’, ‘2’, and ‘6’.
Sample Input 2:
2
Sample Output 2:
1 2
Explanation Of Sample Input 2:
Input: ‘n’ = 2

Output: 1 2

Explanation: Factorial numbers less than or equal to ‘2’ are ‘1’ and ‘2’.
Expected Time Complexity:
The expected time complexity is O(m), where ‘m’ is the number of factorial numbers which are less than or equal to ‘n’.
Expected Space Complexity:
The expected space complexity is O(m), where ‘m’ is the number of factorial numbers which are less than or equal to ‘n’.
Constraints:
1 <= n <= 10^18

Time Limit: 1-sec
Full screen
Console