Skip to main content

Minimum Element After Replacement With Digit Sum - Solution & Explanation

EasyArrayMath14 min readAsked at: Microsoft, Meta, Google
Practice this problem

Problem Statement

You are given an integer array nums.

You replace each element in nums with the sum of its digits.

Return the minimum element in nums after all replacements.

 

Example 1:

Input: nums = [10,12,13,14]

Output: 1

Explanation:

nums becomes [1, 3, 4, 5] after all replacements, with minimum element 1.

Example 2:

Input: nums = [1,2,3,4]

Output: 1

Explanation:

nums becomes [1, 2, 3, 4] after all replacements, with minimum element 1.

Example 3:

Input: nums = [999,19,199]

Output: 10

Explanation:

nums becomes [27, 10, 19] after all replacements, with minimum element 10.

 

Constraints:

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 104

Approach Overview

Problem Overview: You receive an integer array nums. Replace each number with the sum of its digits (for example, 123 → 1+2+3 = 6). After performing this transformation for every element, return the minimum value in the updated array.

The key observation is that the transformation is independent for each number. You only need a digit-sum operation and a way to track the smallest result.

Approach 1: Iterative Approach (O(n · d) time, O(1) space)

Traverse the array and compute the digit sum for each element. The digit sum can be calculated using repeated modulus and division: digit += num % 10 and num /= 10 until the number becomes zero. Replace the current element with this computed value or simply track it in a temporary variable.

After transforming each element, maintain a running minimum. This approach explicitly follows the problem statement: convert every value first, then determine the minimum. Time complexity is O(n · d), where n is the array length and d is the number of digits per number (at most ~10 for typical constraints). Space complexity remains O(1) since only a few variables are used.

This approach works well when you want clarity and step-by-step transformation of the array. It directly demonstrates the digit-sum calculation using simple arithmetic operations.

Approach 2: Optimize using Single Pass (O(n · d) time, O(1) space)

The optimization removes the need to conceptually store the transformed array. Instead, iterate through nums once, compute the digit sum for the current number, and immediately update the minimum value.

For each element: compute the digit sum using modulus and division, compare it with the current minimum, and update the minimum if needed. Because the result is consumed immediately, there is no extra array or replacement step.

This keeps the algorithm linear with O(n · d) time and constant O(1) space. The improvement is mostly conceptual and memory-efficient, but it is typically what interviewers expect because it avoids unnecessary work.

Both solutions rely on basic operations over an array and simple math digit manipulation. No additional data structures are required.

Recommended for interviews: The single-pass approach. It demonstrates that you understand the transformation and can combine computation with aggregation efficiently. Mentioning the straightforward iterative version first shows clarity, but implementing the optimized single-pass scan highlights practical problem-solving skills.

Approach 1: Iterative Approach

In this approach, we will iterate through each number in the array, calculate the sum of its digits, replace the original number with this sum, and finally find the minimum number in the modified array.

This C program defines a helper function digitSum to calculate the sum of digits of a number. In the findMinimumAfterReplacement function, it iterates over the array, applies the digit sum for each element, and finally finds the minimum value among the transformed elements.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n * m), where n is the number of elements in the array, and m is the average number of digits in each number.
Space Complexity: O(1) as we're modifying input array in-place.

Try this approach in the editor →

Approach 2: Optimize using Single Pass

This approach calculates the digit sum for each number and maintains the minimum value seen so far during the iteration. This eliminates the need for a separate pass to find the minimum element after transformation.

This approach initializes the minimum with the digit sum of the first element and iteratively updates it while calculating the digit sum of each subsequent number.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n * m)
Space Complexity: O(1).

Try this approach in the editor →

Approach 3: Simulation

We can traverse the array nums. For each number x, we calculate the sum of its digits y. The minimum value among all y is the answer.

The time complexity is O(n times log M), where n and M are the length of the array nums and the maximum value in the array, respectively. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Iterative Approach

Time Complexity: O(n * m), where n is the number of elements in the array, and m is the average number of digits in each number.
Space Complexity: O(1) as we're modifying input array in-place.

Optimize using Single Pass

Time Complexity: O(n * m)
Space Complexity: O(1).

Simulation

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Iterative TransformationO(n · d)O(1)When implementing the problem exactly as described by replacing every element first
Single Pass Minimum TrackingO(n · d)O(1)Preferred approach in interviews; compute digit sum and update minimum in one traversal

Video Solution

3300. Minimum Element After Replacement With Digit Sum | Leetcode Daily - PythonLeetcode Daily305 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Minimum Element After Replacement With Digit Sum easy or hard?
Minimum Element After Replacement With Digit Sum is classified as an Easy problem. It focuses on basic array traversal and digit manipulation, making it suitable for beginners practicing math-based transformations and simple linear scans.
Minimum Element After Replacement With Digit Sum Python/Java solution
In Python or Java, iterate through the array, compute the digit sum using repeated modulus (% 10) and division (/ 10), and track the smallest value seen. The implementation is short—usually under 10 lines—and runs in O(n · d) time with constant space.
How to solve Minimum Element After Replacement With Digit Sum in O(n)?
Iterate through the array once and compute the digit sum for each number during traversal. Immediately compare the digit sum with the current minimum and update it if smaller. Because each element is processed once and no extra data structures are used, the solution runs in O(n · d) time and O(1) space.
What is the best approach for Minimum Element After Replacement With Digit Sum?
The best approach is a single-pass traversal of the array. For each number, compute its digit sum using modulus and division, then update the current minimum. This keeps the algorithm O(n · d) time where d is the number of digits, and O(1) extra space.
Is Minimum Element After Replacement With Digit Sum asked at Google/Amazon/Meta?
This exact problem is categorized as an easy array and math problem on LeetCode. Variants involving digit sums, digit manipulation, or array transformations appear in screening rounds across companies like Amazon and Google as warm-up or foundational questions.
What data structure is used in Minimum Element After Replacement With Digit Sum?
The problem primarily uses an array for iteration and simple integer arithmetic for computing digit sums. No additional data structures such as hash maps, stacks, or trees are required. The algorithm relies on basic math operations and a running minimum variable.
What is the time complexity of Minimum Element After Replacement With Digit Sum?
The time complexity is O(n · d). The algorithm processes n numbers, and each digit-sum computation takes up to d operations where d is the number of digits in the integer. Since d is small (≤10 for typical constraints), the runtime behaves close to linear O(n).

Ready to solve this problem?

Practice Minimum Element After Replacement With Digit Sum with our built-in code editor and test cases.

Practice on FleetCode