To solve the problem of finding the minimum element after replacing each number in the nums array with the sum of its digits, we can follow these steps:
-
Calculate the Sum of Digits:
For each number in the array, calculate the sum of its digits. For example, the number 123 has digits 1, 2, and 3, and their sum is 6.
-
Replace and Track the Minimum:
As you replace each number with its digit sum, simultaneously track the minimum value encountered.
-
Return the Minimum Value:
After processing all elements, return the smallest digit sum found.
Let's implement this step-by-step in C++.
Step-by-Step Implementation
- Helper Function to Calculate Digit Sum:
It's a good practice to create a helper function that calculates the sum of digits of a given number.
int digitSum(int num) {
int sum = 0;
while (num > 0) {
sum += num % 10; // Add the last digit to sum
num /= 10; // Remove the last digit
}
return sum;
}
- Main Function to Find the Minimum Element After Replacement:
Iterate through the
nums array, replace each element with its digit sum, and keep track of the minimum value.
#include <vector>
#include <climits> // For INT_MAX
class Solution {
public:
int digitSum(int num) {
int sum = 0;
while (num > 0) {
sum += num % 10; // Add the last digit to sum
num /= 10; // Remove the last digit
}
return sum;
}
int minElement(std::vector<int>& nums) {
int minVal = INT32_MAX; // Initialize with the maximum possible integer
for(auto &num : nums){
num = digitSum(num); // Replace with digit sum
if(num < minVal){
minVal = num; // Update minVal if current num is smaller
}
}
return minVal;
}
};
Explanation of the Code
-
digitSum Function:
- Purpose: Calculates the sum of digits of a given number.
- Logic:
- Initialize
sum to 0.
- While the number is greater than
0:
- Add the last digit (
num % 10) to sum.
- Remove the last digit by dividing the number by
10.
- Return the calculated
sum.
-
minElement Function:
- Purpose: Replaces each number in
nums with its digit sum and finds the minimum value.
- Logic:
- Initialize
minVal with the maximum possible integer value to ensure any digit sum will be smaller.
- Iterate through each number in
nums by reference (using auto &num):
- Replace
num with its digit sum by calling digitSum(num).
- If the new
num is smaller than minVal, update minVal.
- After processing all elements, return
minVal.
Example Walkthrough
Let's go through Example 1 to understand how the code works:
- Input:
nums = [10, 12, 13, 14]
Processing:
- Replace
10 with its digit sum: 1 + 0 = 1.
- Replace
12 with its digit sum: 1 + 2 = 3.
- Replace
13 with its digit sum: 1 + 3 = 4.
- Replace
14 with its digit sum: 1 + 4 = 5.
Updated Array: [1, 3, 4, 5]
Minimum Element: 1
Testing the Solution
Let's add some test cases to ensure our solution works as expected.
#include <iostream>
#include <vector>
using namespace std;
// (Include the Solution class here)
int main(){
Solution sol;
// Test Case 1
vector<int> nums1 = {10, 12, 13, 14};
cout << "Test Case 1 Output: " << sol.minElement(nums1) << endl; // Expected: 1
// Test Case 2
vector<int> nums2 = {1, 2, 3, 4};
cout << "Test Case 2 Output: " << sol.minElement(nums2) << endl; // Expected: 1
// Test Case 3
vector<int> nums3 = {999, 19, 199};
cout << "Test Case 3 Output: " << sol.minElement(nums3) << endl; // Expected: 10
return 0;
}
Expected Output:
Test Case 1 Output: 1
Test Case 2 Output: 1
Test Case 3 Output: 10
Conclusion
This solution efficiently replaces each element in the nums array with the sum of its digits while keeping track of the minimum value encountered. The helper function digitSum ensures code modularity and readability. The overall time complexity is O(n * m), where n is the number of elements in nums and m is the number of digits in the largest number, which is acceptable given the problem constraints.