Abbreviating the Product of a Range - Problem

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.
  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.
  3. Finally, represent the product as a string "<pre>...<suf>eC".

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

Input & Output

Example 1 — Small Range
$ Input: left = 1, right = 4
Output: 24e0
💡 Note: Product is 1×2×3×4 = 24. No trailing zeros. Since 24 has 2 digits (≤ 10), return "24e0".
Example 2 — With Trailing Zeros
$ Input: left = 2, right = 5
Output: 12e1
💡 Note: Product is 2×3×4×5 = 120. Remove 1 trailing zero → 12. Since 12 has 2 digits (≤ 10), return "12e1".
Example 3 — Large Range
$ Input: left = 1, right = 1000000000
Output: 28286...00000e35658
💡 Note: Very large product with many trailing zeros. After removing trailing zeros, the number has >10 digits, so we abbreviate as first 5 digits...last 5 digits with exponent.

Constraints

  • 1 ≤ left ≤ right ≤ 104
  • The product can be extremely large

Visualization

Tap to expand
Abbreviating Product of Range [1, 4]1234×××24No trailing zeros, 2 digits ≤ 10Output: "24e0"
Understanding the Visualization
1
Input Range
Range [1, 4] with numbers 1, 2, 3, 4
2
Calculate Product
1 × 2 × 3 × 4 = 24
3
Apply Rules
No trailing zeros, 2 digits ≤ 10, format as "24e0"
Key Takeaway
🎯 Key Insight: Separate handling of trailing zeros and significant digits prevents overflow while maintaining precision
Asked in
Google 45 Microsoft 38
28.5K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen