Maximum Sum Pair with Equal Highest DigitsIntroduction:You are given an array of non-negative integers, where each element represents a number. Your task is to find a pair of numbers from the array such that their sum is maximized and both numbers share the same maximum digit. Write a function maxSumWithEqualMaxDigits(nums) that takes in an array of non-negative integers and returns the maximum sum of a pair of numbers satisfying the given condition. If no such pair exists, return -1. Approach 1: Brute forceJava Code: Input: Output: Code Explanation: - The code starts by importing the java.util.Scanner class to read user input.
- In the main method, the user is prompted to enter the number of elements (n) in the array.
- An array nums of size n is created to store the user-provided elements.
- The user is then prompted to input each element of the array.
- The maxSum function is called with the nums array as an argument to calculate the maximum sum of two numbers with equal maximum digits.
- The maxSum function iterates through all pairs of numbers in the array using two nested loops. For each pair of numbers, it checks if they have equal maximum digits using the hasEqualMaxDigits function.
- The hasEqualMaxDigits function takes two integers as input and converts them to strings. It then compares the maximum digits of these two numbers. If the maximum digits are equal, the function returns true, indicating that the two numbers have equal maximum digits.
- Finally, the maximum sum found is displayed as the output.
Dry Run: Let's do a dry run of the code with the provided input nums = [51, 71, 17, 24, 42] User input: n = 5 User inputs the array elements: [51, 71, 17, 24, 42] Now, let's go through the execution of the maxSum function: Iterating through all pairs of numbers: - Pair (51, 71): Maximum digits are 5 in both numbers. Sum = 51 + 71 = 122
- Pair (51, 17): Maximum digits are 5 in both numbers. Sum = 51 + 17 = 68
- Pair (51, 24): Maximum digits are 5 in both numbers. Sum = 51 + 24 = 75
- Pair (51, 42): Maximum digits are 5 in both numbers. Sum = 51 + 42 = 93
- Pair (71, 17): Maximum digits are 7 in both numbers. Sum = 71 + 17 = 88
- Pair (71, 24): Maximum digits are 7 in both numbers. Sum = 71 + 24 = 95
- Pair (71, 42): Maximum digits are 7 in both numbers. Sum = 71 + 42 = 113
- Pair (17, 24): Maximum digits are 7 in both numbers. Sum = 17 + 24 = 41
- Pair (17, 42): Maximum digits are 7 in both numbers. Sum = 17 + 42 = 59
- Pair (24, 42): Maximum digits are 4 in both numbers. Sum = 24 + 42 = 66
The maximum sum of two numbers with equal maximum digits is 88, which corresponds to the pair (71, 17). Time Complexity: The maxSum function uses two nested loops to iterate through all pairs of numbers. If n is the number of elements in the array, the worst-case time complexity is O(n^2). The hasEqualMaxDigits function operates on strings and has linear time complexity with respect to the lengths of the strings, which are bounded by the number of digits in the input numbers. Therefore, it can be considered to have O(k) time complexity, where k is the maximum number of digits in the input numbers. Space Complexity: The space complexity is mainly determined by the storage of the input array nums, which requires O(n) space. Other variables used in the functions (maxSum, hasEqualMaxDigits) have constant space complexity. Approach 2: Combination of greedy algorithm and dynamic programmingJava Code: Input: Output: Explanation: Code Explanation: - The code starts by importing the java.util.Scanner class to read user input.
- In the main method, the user is prompted to enter the number of elements (n) in the array.
- An array nums of size n is created to store the user-provided elements.
- The user is then prompted to input each element of the array.
- The maxSum function is called with the nums array as an argument to calculate the maximum sum of two numbers with equal maximum digits.
- The maxSum function iterates through all pairs of numbers in the array using two nested loops. For each pair of numbers, it calculates their individual digit frequencies using arrays a and b.
- After calculating the digit frequencies of the two numbers being compared, it checks if there exists a digit res (ranging from 9 to 0) that is present in both numbers.
- If such a digit res exists, it means the two numbers have equal maximum digits. In that case, the maximum sum is updated if the sum of the current pair of numbers is greater than the current maximum sum ans.
- The ans value is eventually returned as the maximum sum of two numbers with equal maximum digits.
Dry Run: Let's perform a dry run of the code with the provided input nums = [51, 71, 17, 24, 42]: User input: n = 5 User inputs the array elements: [51, 71, 17, 24, 42] Now, let's go through the execution of the maxSum function: Iterate through all pairs of numbers: - Pair (51, 71): The maximum digits are 5 and 7, respectively. No common maximum digit. Continue.
- Pair (51, 17): The maximum digits are 5 and 7, respectively. Common maximum digit: 7. Update ans to max(51+17, ans) = max(68, -1) = 68.
- Pair (51, 24): The maximum digits are 5 and 4, respectively. No common maximum digit. Continue.
- Pair (51, 42): The maximum digits are 5 and 4, respectively. No common maximum digit. Continue.
- Pair (71, 17): The maximum digits are 7 and 7, respectively. Common maximum digit: 7. Update ans to max(71+17, ans) = max(88, 68) = 88.
- Pair (71, 24): The maximum digits are 7 and 4, respectively. No common maximum digit. Continue.
- Pair (71, 42): The maximum digits are 7 and 4, respectively. No common maximum digit. Continue.
- Pair (17, 24): The maximum digits are 7 and 4, respectively. No common maximum digit. Continue.
- Pair (17, 42): The maximum digits are 7 and 4, respectively. No common maximum digit. Continue.
- Pair (24, 42): The maximum digits are 4 and 4, respectively. Common maximum digit: 4. Update ans to max(24+42, ans) = max(66, 88) = 88.
The maximum sum of two numbers with equal maximum digits is 88, which corresponds to the pair (71, 17). Time Complexity: The maxSum function uses two nested loops to iterate through all pairs of numbers. If n is the number of elements in the array, the worst-case time complexity is O(n^2). The nested loops involve processing each number's digits using modulo operations and division by 10. The number of digits in each number is bounded by the maximum number of digits in the input. Therefore, the digit processing has a linear time complexity with respect to the number of digits. Space Complexity: The space complexity is mainly determined by the storage of the input array nums, which requires O(n) space. Other variables used in the functions (a, b, ans, res, first, second, ind) are of constant size, contributing to constant space complexity.
|