1006. Clumsy Factorial LeetCode Solution

In this guide, you will get 1006. Clumsy Factorial LeetCode Solution with the best time and space complexity. The solution to Clumsy Factorial problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Clumsy Factorial solution in C++
  4. Clumsy Factorial solution in Java
  5. Clumsy Factorial solution in Python
  6. Additional Resources
1006. Clumsy Factorial LeetCode Solution image

Problem Statement of Clumsy Factorial

The factorial of a positive integer n is the product of all positive integers less than or equal to n.

For example, 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.

For example, 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 <= 104

Complexity Analysis

  • Time Complexity: O(1)
  • Space Complexity: O(1)

1006. Clumsy Factorial LeetCode Solution in C++

class Solution {
 public:
  int clumsy(int n) {
    if (n == 1)
      return 1;
    if (n == 2)
      return 2;
    if (n == 3)
      return 6;
    if (n == 4)
      return 7;
    if (n % 4 == 1)
      return n + 2;
    if (n % 4 == 2)
      return n + 2;
    if (n % 4 == 3)
      return n - 1;
    return n + 1;
  }
};
/* code provided by PROGIEZ */

1006. Clumsy Factorial LeetCode Solution in Java

class Solution {
  public int clumsy(int n) {
    if (n == 1)
      return 1;
    if (n == 2)
      return 2;
    if (n == 3)
      return 6;
    if (n == 4)
      return 7;
    if (n % 4 == 1)
      return n + 2;
    if (n % 4 == 2)
      return n + 2;
    if (n % 4 == 3)
      return n - 1;
    return n + 1;
  }
}
// code provided by PROGIEZ

1006. Clumsy Factorial LeetCode Solution in Python

class Solution:
  def clumsy(self, n: int) -> int:
    if n == 1:
      return 1
    if n == 2:
      return 2
    if n == 3:
      return 6
    if n == 4:
      return 7
    if n % 4 == 1:
      return n + 2
    if n % 4 == 2:
      return n + 2
    if n % 4 == 3:
      return n - 1
    return n + 1
# code by PROGIEZ

Additional Resources

See also  624. Maximum Distance in Arrays LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.