Tuesday, November 19, 2024
Google search engine
HomeData Modelling & AISum of the Product of digits of all Array elements

Sum of the Product of digits of all Array elements

Given an array arr, the task is to find the sum of the product of digits of all array elements

Example:

Input: arr[]={11, 23, 41}
Output: 11
Explanation: 1*1 + 2*3 + 4*1 = 1 + 6 + 4 = 1111

Input: arr[]={46, 32, 78, 0}
Output: 86 

 

Approach: To solve this problem, find the product of digits of all numbers and then just add them up. Follow the below steps to solve this problem:

  1. Create a function findProduct which will get a number and find the product of its digits.
  2. Create a variable sum to store the final answer and initialise it with 0.
  3. Now, traverse the array and for each element:
    • Pass it to the function findProduct and get the product of its digit.
    • Then add its product of digit to sum.
  4. Return sum as the final answer.

Below is the implementation of the above approach:

C++




// C++ code for the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the product of the
// digits of a number N
int findProduct(int N)
{
    if (N == 0) {
        return 0;
    }
  
    int product = 1;
    while (N > 0) {
        product *= (N % 10);
        N /= 10;
    }
  
    return product;
}
  
// Function to find the sum of the product of
// digits of all array elements
int sumOfProduct(vector<int> arr)
{
  
    int sum = 0;
    for (auto x : arr) {
        sum += findProduct(x);
    }
  
    return sum;
}
  
// Driver Code
int main()
{
    vector<int> arr = { 46, 32, 78, 0 };
    cout << sumOfProduct(arr);
}


Java




// Java code to implement above approach
import java.util.*;
public class GFG {
  
// Function to find the product of the
// digits of a number N
static int findProduct(int N)
{
    if (N == 0) {
        return 0;
    }
  
    int product = 1;
    while (N > 0) {
        product *= (N % 10);
        N /= 10;
    }
  
    return product;
}
  
// Function to find the sum of the product of
// digits of all array elements
static int sumOfProduct(int []arr)
{
  
    int sum = 0;
    for (int i = 0; i < arr.length; i++) {
        sum += findProduct(arr[i]);
    }
  
    return sum;
}
  
// Driver code
public static void main(String args[])
{
    int []arr = { 46, 32, 78, 0 };
    System.out.println(sumOfProduct(arr));
  
}
}
  
// This code is contributed by Samim Hossain Mondal.


Python3




# Python code for the above approach
  
# Function to find the product of the
# digits of a number N
def findProduct(N):
    if (N == 0):
        return 0
  
    product = 1
    while (N > 0):
        product *= (N % 10)
        N = N // 10
    return product
  
# Function to find the sum of the product of
# digits of all array elements
def sumOfProduct(arr):
  
    sum = 0
    for x in arr:
        sum += findProduct(x)
  
    return sum
  
# Driver Code
arr = [46, 32, 78, 0]
print(sumOfProduct(arr))
  
# This code is contributed by Saurabh Jaiswal


C#




// C# code to implement above approach
using System;
class GFG {
  
// Function to find the product of the
// digits of a number N
static int findProduct(int N)
{
    if (N == 0) {
        return 0;
    }
  
    int product = 1;
    while (N > 0) {
        product *= (N % 10);
        N /= 10;
    }
  
    return product;
}
  
// Function to find the sum of the product of
// digits of all array elements
static int sumOfProduct(int []arr)
{
  
    int sum = 0;
    for (int i = 0; i < arr.Length; i++) {
        sum += findProduct(arr[i]);
    }
  
    return sum;
}
  
// Driver code
public static void Main()
{
    int []arr = { 46, 32, 78, 0 };
    Console.Write(sumOfProduct(arr));
  
}
}
  
// This code is contributed by Samim Hossain Mondal.


Javascript




<script>
    // JavaScript code for the above approach
  
    // Function to find the product of the
    // digits of a number N
    function findProduct(N) {
      if (N == 0) {
        return 0;
      }
  
      let product = 1;
      while (N > 0) {
        product *= (N % 10);
        N = Math.floor(N / 10)
      }
      return product;
    }
  
    // Function to find the sum of the product of
    // digits of all array elements
    function sumOfProduct(arr) {
  
      let sum = 0;
      for (let x of arr) {
        sum += findProduct(x);
      }
  
      return sum;
    }
  
    // Driver Code
    let arr = [46, 32, 78, 0];
    document.write(sumOfProduct(arr));
  
  // This code is contributed by Potta Lokesh
  </script>


 
 

Output: 

86

 

Time Complexity: O(NlogM), where N is the size of the array and M is the maximum number in the array
Auxiliary Space: O(1)

 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments