The factorial of a positive integer n is the product of all positive integers less than or equal to n.
factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1.We make a clumsy factorial using the integers in decreasing order by swapping out the multiply operations for a fixed rotation of operations with multiply '*', divide '/', add '+', and subtract '-' in this order.
clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1.However, these operations are still applied using the usual order of operations of arithmetic. We do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right.
Additionally, the division that we use is floor division such that 10 * 9 / 8 = 90 / 8 = 11.
Given an integer n, return the clumsy factorial of n.
Example 1:
Input: n = 4 Output: 7 Explanation: 7 = 4 * 3 / 2 + 1
Example 2:
Input: n = 10 Output: 12 Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1
Constraints:
1 <= n <= 104The Clumsy Factorial modifies the traditional factorial by applying a repeating sequence of operations: *, /, +, and - on decreasing integers. Because operator precedence matters, multiplication and division must be evaluated before addition and subtraction. A common way to handle this is by using a stack-based simulation.
Start from n and iterate downward while cycling through the operations. Push numbers to a stack, applying multiplication or division immediately with the top element to respect precedence. For addition, push the next value, and for subtraction, push the negative of the value. At the end, summing the stack gives the final result.
This approach cleanly simulates operator precedence and keeps the implementation straightforward. The algorithm processes each number once, giving a linear time complexity. Space is used to maintain the stack during computation.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Stack-based Simulation | O(n) | O(n) |
| Optimized Mathematical Observation | O(1) | O(1) |
ThePrimeTime
Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Yes, with mathematical observation you can derive patterns in the results for different values of n. This allows an O(1) solution, but it requires identifying repeating behaviors rather than directly simulating the operations.
Yes, problems like Clumsy Factorial appear in technical interviews because they test simulation, operator precedence handling, and data structure usage. They are especially useful for evaluating a candidate’s understanding of stacks and arithmetic logic.
The most common approach is to simulate the operations using a stack while iterating from n down to 1. Multiplication and division are applied immediately to maintain operator precedence, while addition and subtraction are handled by pushing values to the stack. Finally, summing the stack gives the result.
A stack is ideal because it helps manage operator precedence between multiplication, division, addition, and subtraction. It allows immediate evaluation of high‑precedence operations while postponing addition and subtraction until the end.