Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# 3300. Minimum Element After Replacement With Digit Sum

[LeetCode Link](https://leetcode.com/problems/minimum-element-after-replacement-with-digit-sum/)

Difficulty: Easy
Topics: Array, Math
Acceptance Rate: 87.1%

## Hints

### Hint 1

Read the problem carefully. You only need two things: a way to transform each element, and a way to track the smallest transformed value. No fancy data structure is needed here — think about what a single linear pass through the array can accomplish.

### Hint 2

The transformation is "replace a number with the sum of its decimal digits." How do you extract digits from an integer in any language? Repeatedly take the value modulo 10 (the last digit) and divide by 10 (drop the last digit) until the value reaches 0.

### Hint 3

You do not need to actually rewrite the array. Compute the digit sum for each element on the fly, compare it against the running minimum, and update the minimum if smaller. Initialize the minimum to a safely large value (or to the digit sum of the first element) so the very first comparison works correctly.

## Approach

Walk the array once. For each element `n`, compute its digit sum by looping:

1. Set `sum = 0`.
2. While `n > 0`, add `n % 10` to `sum`, then set `n = n / 10`.
3. After the inner loop, `sum` is the digit sum of the original number.

Keep a `best` variable that holds the minimum digit sum seen so far. Update it whenever the freshly computed `sum` is smaller. After processing every element, return `best`.

Example walk-through with `nums = [999, 19, 199]`:

- `999` → `9 + 9 + 9 = 27`, `best = 27`.
- `19` → `1 + 9 = 10`, `best = 10`.
- `199` → `1 + 9 + 9 = 19`, `best` stays `10`.

Return `10`, matching the expected output.

This works because the minimum-of-transformed-values is independent of order — we never need to look back at earlier elements, so a single streaming pass is sufficient.

## Complexity Analysis

Time Complexity: O(n · d), where `n = len(nums)` and `d` is the number of decimal digits per element. Since `nums[i] <= 10^4`, `d <= 5`, so this is effectively O(n).
Space Complexity: O(1) — only a few integer accumulators are used; the input array is not modified.

## Edge Cases

- **Single-element array**: With `len(nums) == 1`, the answer is simply the digit sum of that one element. Initializing `best` correctly (e.g., to the first element's digit sum or to a sentinel) avoids returning a stale default.
- **All single-digit numbers**: When every `nums[i] < 10`, each digit sum equals the original number, so the answer is just `min(nums)`. The same code path handles this without any special case.
- **Numbers with trailing zeros (e.g., 10, 100, 1000)**: The `n % 10` / `n /= 10` loop correctly skips zero contributions; `10` becomes `1`, `100` becomes `1`, etc.
- **Largest allowed value (10000)**: Digit sum is `1`. The loop must continue until `n` is exactly `0`, not `<= 0`, to handle this cleanly.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
number: "3300"
frontend_id: "3300"
title: "Minimum Element After Replacement With Digit Sum"
slug: "minimum-element-after-replacement-with-digit-sum"
difficulty: "Easy"
topics:
- "Array"
- "Math"
acceptance_rate: 8709.0
is_premium: false
created_at: "2026-05-29T04:51:25.405449+00:00"
fetched_at: "2026-05-29T04:51:25.405449+00:00"
link: "https://leetcode.com/problems/minimum-element-after-replacement-with-digit-sum/"
date: "2026-05-29"
---

# 3300. Minimum Element After Replacement With Digit Sum

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`
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

// Approach: single linear pass. For each element, compute its decimal digit
// sum by repeated mod-10 / div-10, and track the minimum digit sum seen so
// far. O(n * d) time where d is the number of digits (<= 5 here), O(1) space.

func minElementDaily20260529(nums []int) int {
best := -1
for _, n := range nums {
sum := 0
for n > 0 {
sum += n % 10
n /= 10
}
if best == -1 || sum < best {
best = sum
}
}
return best
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import "testing"

func TestMinElementDaily20260529(t *testing.T) {
tests := []struct {
name string
nums []int
expected int
}{
{"example 1: mixed two-digit numbers", []int{10, 12, 13, 14}, 1},
{"example 2: already single digits", []int{1, 2, 3, 4}, 1},
{"example 3: three-digit numbers", []int{999, 19, 199}, 10},
{"edge case: single element", []int{7}, 7},
{"edge case: maximum constraint value", []int{10000}, 1},
{"edge case: all identical elements", []int{55, 55, 55}, 10},
{"edge case: minimum hides behind large input", []int{9999, 9999, 100, 9999}, 1},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := minElementDaily20260529(tt.nums)
if got != tt.expected {
t.Errorf("minElementDaily20260529(%v) = %d, want %d", tt.nums, got, tt.expected)
}
})
}
}