Maximum pair sum such that their digit sum are equal and divisible by K
Given a positive integer array arr[] of size N (1 ≤ N ≤ 105) and a positive integer K, the task is to find the maximum value of arr[i] + arr[j] by choosing two indices i and j, such that i != j, and the sum of digits of the element at arr[i] should be equal to arr[j] and are divisible by K.
Examples:
Input: arr = [18, 43, 36, 13, 7]
Output: 54
Explanation:The pairs (i, j) that satisfy the conditions are:
Index (0, 2), both numbers have a sum of digits equal to 9, and their sum is 18 + 36 = 54.
Index (1, 4), both numbers have a sum of digits equal to 7, and their sum is 43 + 7 = 50.
So the maximum sum that we can obtain is 54.Input: arr = [10, 12, 19, 14]
Output: -1
Explanation: There are no two numbers that satisfy the conditions, so we return -1
An approach using Hashing:
Use map to store all elements having same digit sum in sorted order and then again iterate over the map for each different digit sum which are divisible by K and maximize result with two greatest elements.
Follow the steps below to implement the above idea:
- Initialize a map for mapping elements that have the same digit sum and Keep the value of the map in sorted order.
- Iterating on the given array
- Initialize a variable sum for calculating the sum of digits
- Calculate the sum of the digit of element arr[i]
- Insert an element into the map having the sum of digits in “sum”
- Initialize a variable result for storing the maximum sum possible
- Iterate over the map
- Check if the digit sum is divisible by K and the value of the key is greater than 1 (it’ll ensure that there are at least two elements whose digit sum is divisible by K).
- Maximize the result by summation of the last two elements that are stored in the map
- Check if the digit sum is divisible by K and the value of the key is greater than 1 (it’ll ensure that there are at least two elements whose digit sum is divisible by K).
- Return the result
Below is the implementation of the above approach.
C++
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Given a positive integer array arr[] of size N (1 ≤ N ≤ 105) and a positive integer K, the task is to find the maximum value of arr[i] + arr[j] by choosing two indices i and j, such that i != j, and the sum of digits of the element at arr[i] should be equal to arr[j] and are divisible by K.
Examples:
Input: arr = [18, 43, 36, 13, 7]
Output: 54
Explanation:The pairs (i, j) that satisfy the conditions are:
Index (0, 2), both numbers have a sum of digits equal to 9, and their sum is 18 + 36 = 54.
Index (1, 4), both numbers have a sum of digits equal to 7, and their sum is 43 + 7 = 50.
So the maximum sum that we can obtain is 54.Input: arr = [10, 12, 19, 14]
Output: -1
Explanation: There are no two numbers that satisfy the conditions, so we return -1
An approach using Hashing:
Use map to store all elements having same digit sum in sorted order and then again iterate over the map for each different digit sum which are divisible by K and maximize result with two greatest elements.
Follow the steps below to implement the above idea:
- Initialize a map for mapping elements that have the same digit sum and Keep the value of the map in sorted order.
- Iterating on the given array
- Initialize a variable sum for calculating the sum of digits
- Calculate the sum of the digit of element arr[i]
- Insert an element into the map having the sum of digits in “sum”
- Initialize a variable result for storing the maximum sum possible
- Iterate over the map
- Check if the digit sum is divisible by K and the value of the key is greater than 1 (it’ll ensure that there are at least two elements whose digit sum is divisible by K).
- Maximize the result by summation of the last two elements that are stored in the map
- Check if the digit sum is divisible by K and the value of the key is greater than 1 (it’ll ensure that there are at least two elements whose digit sum is divisible by K).
- Return the result
Below is the implementation of the above approach.
C++
|
Time Complexity: O(N)
Auxiliary Space: O(N)