Skip to main content

Abbreviating the Product of a Range - Solution & Explanation

HardMath11 min readAsked at: Avalara
Practice this problem

Problem Statement

You are given two positive integers left and right with left <= right. Calculate the product of all integers in the inclusive range [left, right].

Since the product may be very large, you will abbreviate it following these steps:

  1. Count all trailing zeros in the product and remove them. Let us denote this count as C.
    • For example, there are 3 trailing zeros in 1000, and there are 0 trailing zeros in 546.
  2. Denote the remaining number of digits in the product as d. If d > 10, then express the product as <pre>...<suf> where <pre> denotes the first 5 digits of the product, and <suf> denotes the last 5 digits of the product after removing all trailing zeros. If d <= 10, we keep it unchanged.
    • For example, we express 1234567654321 as 12345...54321, but 1234567 is represented as 1234567.
  3. Finally, represent the product as a string "<pre>...<suf>eC".
    • For example, 12345678987600000 will be represented as "12345...89876e5".

Return a string denoting the abbreviated product of all integers in the inclusive range [left, right].

 

Example 1:

Input: left = 1, right = 4
Output: "24e0"
Explanation: The product is 1 × 2 × 3 × 4 = 24.
There are no trailing zeros, so 24 remains the same. The abbreviation will end with "e0".
Since the number of digits is 2, which is less than 10, we do not have to abbreviate it further.
Thus, the final representation is "24e0".

Example 2:

Input: left = 2, right = 11
Output: "399168e2"
Explanation: The product is 39916800.
There are 2 trailing zeros, which we remove to get 399168. The abbreviation will end with "e2".
The number of digits after removing the trailing zeros is 6, so we do not abbreviate it further.
Hence, the abbreviated product is "399168e2".

Example 3:

Input: left = 371, right = 375
Output: "7219856259e3"
Explanation: The product is 7219856259000.

 

Constraints:

  • 1 <= left <= right <= 104

Approach Overview

Problem Overview: Given two integers left and right, compute the product of all numbers in the range [left, right]. The full product grows extremely large, so the output must be abbreviated: keep the first few digits, the last few digits, and represent the number of trailing zeros using scientific-style notation.

Approach 1: Multiplicative Approach with Optimization for Large Numbers (O(n) time, O(1) space)

Iterate through every integer from left to right and maintain a running product. Direct multiplication quickly overflows standard integer ranges, so you track only the significant prefix and suffix. After each multiplication, strip trailing zeros by dividing by 10 and count how many zeros were removed. Keep the last digits using a modulo (commonly 10^10) so the suffix stays bounded, and trim the prefix whenever it grows beyond a safe digit limit. This technique preserves enough information to reconstruct the abbreviated format while avoiding massive integers. Time complexity is O(n) because you process each number once, and space complexity is O(1). The logic relies heavily on careful numeric manipulation, making it a good exercise in math and large-number handling.

Approach 2: Logarithmic Reduction to Avoid Overflow (O(n) time, O(1) space)

Instead of maintaining the full prefix directly, compute the leading digits using logarithms. The key observation: log10(a × b) = log10(a) + log10(b). Sum log10(i) for every i in the range to estimate the magnitude of the product. The fractional part of this log value determines the leading digits via exponentiation. In parallel, track the suffix digits using modular multiplication while removing trailing zeros the same way as in the previous approach. This separates the concerns: logs estimate the prefix, while modular arithmetic maintains the suffix. Time complexity remains O(n) and space complexity O(1). This approach is common in problems involving extremely large numbers and scientific notation, often appearing in math or number theory contexts.

Recommended for interviews: The multiplicative trimming approach is usually preferred. It demonstrates that you understand how to control integer growth, remove trailing zeros, and keep both prefix and suffix digits without overflow. The logarithmic method shows deeper numerical insight and is useful when the prefix must be derived analytically rather than maintained directly.

Approach 1: Multiplicative Approach with Optimization for Large Numbers

This approach involves calculating the product directly by multiplying each number in the range from left to right, while avoiding overflow by noticing that every pair of 2 and 5 contribute to a trailing zero.

Efficiently count trailing zeros by tracking the number of times 2 and 5 appear as factors. Then manage the size of the result by only maintaining its significant parts.

This solution first counts the number of factors of two and five in each integer, which allows it to determine the number of trailing zeros. Then it calculates two separate parts of the product to avoid overflow:

  • front_product: captures the significant digits by continuously scaling down to avoid exponential growth.
  • back_product: captures the final few digits by taking the result modulo 10^5.

The final output depends on the size of the number (num of digits) and is concatenated accordingly.

Code

Python

C++

Complexity

Time Complexity: O(right - left + 1), as it iterates through each integer in the range once.

Space Complexity: O(1), meaning it uses a constant amount of extra space regardless of the input size.

Try this approach in the editor →

Approach 2: Logarithmic Reduction to Avoid Overflow

An alternative approach is to use logarithmic properties to calculate the number of digits without constructing the full number, making it suitable for very large ranges.

We calculate the product in logarithmic space to avoid overflow and count trailing zeros as before. This method relies on properties of logarithms to mimic multiplication.

Instead of keeping track of the large product directly, this implementation first calculates the logarithm of each number, which prevents overflow. We construct the logarithm of the entire product and add the counts of 2 and 5 to appropriately manage trailing zeros.

The final step projects the size of significant digits from the logarithmic value obtained.

Code

Java

JavaScript

Complexity

Time Complexity: O(right - left + 1).

Space Complexity: O(1).

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Multiplicative Approach with Optimization for Large Numbers

Time Complexity: O(right - left + 1), as it iterates through each integer in the range once.

Space Complexity: O(1), meaning it uses a constant amount of extra space regardless of the input size.

Logarithmic Reduction to Avoid Overflow

Time Complexity: O(right - left + 1).

Space Complexity: O(1).

Default Approach

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Multiplicative Approach with Prefix/Suffix TrimmingO(n)O(1)General solution when you want direct control over trailing zeros and digit trimming during multiplication.
Logarithmic Reduction with Modular Suffix TrackingO(n)O(1)Useful when the leading digits are easier to derive from logarithmic magnitude rather than storing large prefixes.

Video Solution

2117. Abbreviating the Product of a Range (Leetcode Hard)Programming Live with Larry257 views views

Watch 2 more video solutions →

Frequently Asked Questions

Is Abbreviating the Product of a Range easy or hard?
LeetCode classifies this problem as Hard. The difficulty comes from handling extremely large products, maintaining both leading and trailing digits, and counting trailing zeros accurately without computing the full number.
Abbreviating the Product of a Range Python/Java solution
Python and C++ implementations typically use the multiplicative trimming approach because large integers are easier to manage. Java and JavaScript solutions often combine logarithmic calculations for leading digits with modular arithmetic for suffix digits. All versions run in O(n) time with constant extra space.
How to solve Abbreviating the Product of a Range in O(n)?
Iterate from left to right multiplying into a running product. Remove trailing zeros by repeatedly dividing by 10 and count them. Keep the suffix bounded with modulo (for example 10^10) and trim the prefix digits if the number becomes too large. Alternatively, compute leading digits using logarithms while tracking the suffix with modular multiplication.
What is the best approach for Abbreviating the Product of a Range?
The most practical solution multiplies numbers from left to right while trimming trailing zeros and limiting the stored digits. Maintain a suffix using modulo arithmetic and track leading digits by trimming the product when it becomes too large. This runs in O(n) time and O(1) space and avoids large integer overflow.
Is Abbreviating the Product of a Range asked at Google/Amazon/Meta?
Problems involving large-number multiplication, trailing zero counting, and digit abbreviation frequently appear in interviews at companies like Google, Amazon, and Meta. The exact problem may vary, but the same techniques—logarithms, modular arithmetic, and zero counting—are commonly tested.
What data structure is used in Abbreviating the Product of a Range?
No complex data structure is required. The solution mainly uses arithmetic operations, modular arithmetic for suffix digits, and logarithmic calculations to estimate leading digits. The focus is on numerical manipulation rather than arrays, trees, or hash maps.
What is the time complexity of Abbreviating the Product of a Range?
Both common solutions run in O(n) time where n = right − left + 1 because each number in the range is processed once. Space complexity is O(1) since only a few numeric variables are maintained for prefix digits, suffix digits, and trailing zero counts.

Ready to solve this problem?

Practice Abbreviating the Product of a Range with our built-in code editor and test cases.

Practice on FleetCode