Find the maximum sum of digits of the product of two numbers

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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..

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right



Output:

6


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.