Given an array of integers arr[], the task is to maximize the product of the digit sum of every consecutive pairs in a subsequence of length K.
Note: K is always even because pairs will be formed on the even length.
Examples:
Input:_ arr[] = {2, 100, 99, 3, 16}, K = 4_
Output:_ 128_
The optimal subsequence of length 4 is **[2, 100, 99, 16]**
Sum of digits = 2, 2, 18 and 7 respectively.
So Product of digit sums in pairs = 2 * 1 + 18 * 7 = 2 + 126 = 128, which is maximum.
Input:_ arr[] = {10, 5, 9, 101, 24, 2, 20, 14}, K = 6_
Output:_ 69_
The optimal subsequence of length 6 = **[10, 5, 9, 24, 2, 14]**
Sum of digits = 1, 5, 9, 6, 2 and 5 respectively.
So Product of digit sums in pairs = 1 * 5 + 9 * 6 + 2 * 5 = 5 + 54 + 10 = 69, which is maximum.
Approach: The idea is to use Dynamic Programming. As we need to find pairs in the array by including or excluding some of the elements from the array to form a subsequence. So let DP[i][j][k] be our dp array which stores the maximum product of the sum of digits of the elements upto index** i** having length** j **and last element as K.
Observations:
#arrays #dynamic programming #mathematical #recursion #number-digits #subsequence