Given an array arr[] of size N( > 2). The task is to find the maximum sum of digits of the product of any two numbers of the given array.
Examples:
Input : arr[] = {8, 7}
Output : 11
The product of 8 and 7 is 56. The sum of the digits of 56 is equal to 11.Input : arr[] = {4, 3, 5}
Output : 6
Product of 4 & 3 = 12. Sum of the digits = 3.
Product of 3 & 5 = 15. Sum of the digits = 6.
Product of 4 & 5 = 20. Sum of the digits = 2.
Approach: Run nested loops to select two numbers of the array and get the product. For every product check the digit sum and find the maximum digit sum.
Below is the implementation of the above approach:
C++
// C++ program find the maximum sum of // digits of the product of two numbers #include <bits/stdc++.h> using namespace std; // Function to find the sum of the digits int sumDigits(int n) { int digit_sum = 0; while (n) { digit_sum += n % 10; n /= 10; } return digit_sum; } // Function to find the maximum sum of digits of product int productOfNumbers(int arr[], int n) { int sum = INT_MIN; // Run nested loops for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { int product = arr[i] * arr[j]; // Find the maximum sum sum = max(sum, sumDigits(product)); } } // Return the required answer return sum; } // Driver code int main() { int arr[] = { 4, 3, 5 }; int n = sizeof(arr) / sizeof(arr[0]); cout << productOfNumbers(arr, n); return 0; } |
Java
// Java program find the maximum sum of // digits of the product of two numbers import java.io.*; class GFG { // Function to find the sum of the digits static int sumDigits(int n) { int digit_sum = 0; while (n > 0) { digit_sum += n % 10; n /= 10; } return digit_sum; } // Function to find the maximum sum // of digits of product static int productOfNumbers(int []arr, int n) { int sum = Integer.MIN_VALUE; // Run nested loops for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { int product = arr[i] * arr[j]; // Find the maximum sum sum = Math.max(sum, sumDigits(product)); } } // Return the required answer return sum; } // Driver code public static void main (String[] args) { int []arr = { 4, 3, 5 }; int n = arr.length; System.out.print( productOfNumbers(arr, n)); } } // This code is contributed by anuj_67.. |
Python3
# Python3 program find the maximum sum of # digits of the product of two numbers import sys # Function to find the sum of the digits def sumDigits(n): digit_sum = 0; while (n > 0): digit_sum += n % 10; n /= 10; return digit_sum; # Function to find the maximum sum # of digits of product def productOfNumbers(arr, n): sum = -sys.maxsize - 1; # Run nested loops for i in range(n - 1): for j in range(i + 1, n): product = arr[i] * arr[j]; # Find the maximum sum sum = max(sum, sumDigits(product)); # Return the required answer return sum; # Driver code if __name__ == '__main__': arr =[ 4, 3, 5 ]; n = len(arr); print(int(productOfNumbers(arr, n))); # This code contributed by PrinciRaj1992 |
C#
// C# program find the maximum sum of // digits of the product of two numbers using System; class GFG { // Function to find the sum of the digits static int sumDigits(int n) { int digit_sum = 0; while (n > 0) { digit_sum += n % 10; n /= 10; } return digit_sum; } // Function to find the maximum sum // of digits of product static int productOfNumbers(int []arr, int n) { int sum = int.MinValue; // Run nested loops for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { int product = arr[i] * arr[j]; // Find the maximum sum sum = Math.Max(sum, sumDigits(product)); } } // Return the required answer return sum; } // Driver code public static void Main (String[] args) { int []arr = { 4, 3, 5 }; int n = arr.Length; Console.Write(productOfNumbers(arr, n)); } } // This code is contributed by 29AjayKumar |
Output:
6
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Find maximum product of digits among numbers less than or equal to N
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Number of digits in the product of two numbers
- Minimum digits to be removed to make either all digits or alternating digits same
- Maximum of sum and product of digits until number is reduced to a single digit
- Maximum sum and product of the M consecutive digits in a number
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Find last k digits in product of an array numbers
- Smallest number with given sum of digits and sum of square of digits
- Find smallest number with given number of digits and sum of digits
- Find the Largest number with given number of digits and sum of digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Maximum Product Subarray | Added negative product case
- Minimum number of digits to be removed so that no two consecutive digits are same
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Sort the numbers according to their product of digits
- Cumulative product of digits of all numbers in the given range
- Divide a number into two parts such that sum of digits is maximum
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

