You are given an integer array nums containing positive integers. We define a function encrypt such that encrypt(x) replaces every digit in x with the largest digit in x. For example, encrypt(523) = 555 and encrypt(213) = 333.Return the sum of encrypted elements. Example 1:Input: nums = [1,2,3]Output: 6Explanation: The encrypted elements are [1,2,3]. The sum of encrypted elements is 1 + 2 + 3 == 6.Example 2:Input: nums = [10,21,31]Output: 66Explanation: The encrypted elements are [11,22,33]. The sum of encrypted elements is 11 + 22 + 33 == 66. Constraints:1 <= nums.length <= 501 <= nums[i] <= 1000C++ 1class Solution {2public:3 int sumOfEncryptedInt(vector<int>& nums) {4 5 }6};
Question
You are given an integer array nums containing positive integers. We define a function encrypt such that encrypt(x) replaces every digit in x with the largest digit in x. For example, encrypt(523) = 555 and encrypt(213) = 333.Return the sum of encrypted elements. Example 1:Input: nums = [1,2,3]Output: 6Explanation: The encrypted elements are [1,2,3]. The sum of encrypted elements is 1 + 2 + 3 == 6.Example 2:Input: nums = [10,21,31]Output: 66Explanation: The encrypted elements are [11,22,33]. The sum of encrypted elements is 11 + 22 + 33 == 66. Constraints:1 <= nums.length <= 501 <= nums[i] <= 1000C++ 1class Solution {2public:3 int sumOfEncryptedInt(vector<int>& nums) {4 5 }6};
Solution
Here is the C++ solution for the problem:
class Solution {
public:
int sumOfEncryptedInt(vector<int>& nums) {
int sum = 0;
for(int num : nums) {
int maxDigit = 0;
while(num) {
maxDigit = max(maxDigit, num % 10);
num /= 10;
}
sum += maxDigit * pow(10, to_string(num).length() - 1);
}
return sum;
}
};
This solution works by iterating over each number in the input array. For each number, it finds the maximum digit by repeatedly taking the remainder of the number divided by 10 (which gives the last digit) and updating the maximum digit if necessary. It then adds the maximum digit times the number of digits in the original number to the sum. This effectively "encrypts" the number by replacing each digit with the maximum digit. The sum of all encrypted numbers is then returned.
Similar Questions
You are given an array of positive integers nums.Alice and Bob are playing a game. In the game, Alice can choose either all single-digit numbers or all double-digit numbers from nums, and the rest of the numbers are given to Bob. Alice wins if the sum of her numbers is strictly greater than the sum of Bob's numbers.Return true if Alice can win this game, otherwise, return false. Example 1:Input: nums = [1,2,3,4,10]Output: falseExplanation:Alice cannot win by choosing either single-digit or double-digit numbers.Example 2:Input: nums = [1,2,3,4,5,14]Output: trueExplanation:Alice can win by choosing single-digit numbers which have a sum equal to 15.Example 3:Input: nums = [5,5,5,25]Output: trueExplanation:Alice can win by choosing double-digit numbers which have a sum equal to 25
Given an integer array nums, find the subarray with the largest sum, and return its sum.
Given an integer array nums, return true if you can partition the array into two subsets such that the sum of the elements in both subsets is equal or false otherwise. Example 1:Input: nums = [1,5,11,5]Output: trueExplanation: The array can be partitioned as [1, 5, 5] and [11].Example 2:Input: nums = [1,2,3,5]Output: falseExplanation: The array cannot be partitioned into equal sum subsets. Constraints:1 <= nums.length <= 2001 <= nums[i] <= 100
You are given an integer array nums sorted in non-decreasing order.Build and return an integer array result with the same length as nums such that result[i] is equal to the summation of absolute differences between nums[i] and all the other elements in the array.In other words, result[i] is equal to sum(|nums[i]-nums[j]|) where 0 <= j < nums.length and j != i (0-indexed). Example 1:Input: nums = [2,3,5]Output: [4,3,5]Explanation: Assuming the arrays are 0-indexed, thenresult[0] = |2-2| + |2-3| + |2-5| = 0 + 1 + 3 = 4,result[1] = |3-2| + |3-3| + |3-5| = 1 + 0 + 2 = 3,result[2] = |5-2| + |5-3| + |5-5| = 3 + 2 + 0 = 5.Example 2:Input: nums = [1,4,6,8,10]Output: [24,15,13,15,21]
Java program to sum values of an array.Write a Java program to sum values of an array. Constraints:N/AExample:Input:1, 2, 3, 4, 5, 6, 7, 8, 9, 10Output:55Explanation:-Public Test Cases:# INPUT EXPECTED OUTPUT1 1, 2, 3, 4, 5, 6, 7, 8, 9, 1055
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.