Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Preimage Size of Factorial Zeroes Function

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "793" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

Let f(x) be the number of zeroes at the end of x!. Recall that x! = 1 2 3 ... x and by convention, 0! = 1. For example, f(3) = 0 because 3! = 6 has no zeroes at the end, while f(11) = 2 because 11! = 39916800 has two zeroes at the end. Given an integer k, return the number of non-negative integers x have the property that f(x) = k. Example 1: Input: k = 0 Output: 5 Explanation: 0!, 1!, 2!, 3!, and 4! end with k = 0 zeroes. Example 2: Input: k = 5 Output: 0 Explanation: There is no x such that x! ends in k = 5 zeroes. Example 3: Input: k = 3 Output: 5 Constraints: 0 <= k <= 109

Explanation

Here's the breakdown of the solution:

  • Understanding Trailing Zeroes: The number of trailing zeroes in x! is determined by the number of times 5 appears as a factor in the numbers from 1 to x. This is because the number of factors of 2 will always be greater than or equal to the number of factors of 5, and a factor of 2 and a factor of 5 combine to create a factor of 10, which contributes to a trailing zero.
  • Monotonicity: The function f(x) is monotonically increasing (non-decreasing). This means that as x increases, the number of trailing zeroes can only increase or stay the same. This property allows us to use binary search to efficiently find the smallest x such that f(x) = k.
  • Finding the Range: Since f(x) is monotonic, if we find the smallest x such that f(x) = k, then the next 5 values of x will also have f(x) = k, until we reach an x where a new factor of 5 appears in the factorial. Thus, we can find the smallest x1 such that f(x1) = k, and the smallest x2 such that f(x2) = k+1, and the answer will be x2 - x1.

  • Time & Space Complexity: O(log2k) time complexity, O(1) space complexity. The outer binary search for k and k+1 is O(log k). The zeros function which does the calculation for the number of trailing zeroes given a particular x is O(log x). Since x is in the ballpark of k, the zeros is O(log k) as well. Thus O(log k) * O(log k) = O(log2k). Constant space is used.

Code

    def trailingZeroes(n):
    count = 0
    i = 5
    while n // i >= 1:
        count += n // i
        i *= 5
    return count

def find_k(k):
    low = 0
    high = 5 * k  # Upper bound for x (since every 5th number contributes a factor of 5)

    while low <= high:
        mid = (low + high) // 2
        zeroes = trailingZeroes(mid)

        if zeroes < k:
            low = mid + 1
        elif zeroes > k:
            high = mid - 1
        else:
            # Found a potential solution. Need to find the smallest one.
            high = mid -1
    return low

class Solution:
    def preimageSizeFZF(self, k: int) -> int:
        x1 = find_k(k)
        x2 = find_k(k+1)

        return x2-x1

More from this blog

C

Chatmagic blog

2894 posts

Solving Leetcode Interviews in Seconds with AI: Preimage Size of Factorial Zeroes Function