Sum and Product of all even digit sum Nodes of a Singly Linked List
Given a singly linked list containing N nodes, the task is to find the sum and product of all the nodes from the list whose data value has an even digit sum.
Examples:
Input: 15 -> 16 -> 8 -> 6 -> 13
Output:
Sum = 42
Product = 9360
Explanation:
The sum of all digit of number in linked list are:
15 = 1 + 5 = 6
16 = 1 + 6 = 7
8 = 8
6 = 6
13 = 1 + 3 = 4
The list contains 4 Even Digit Sum data values 15, 8, 6 and 13.
Sum = 15 + 8 + 6 + 13 = 42
Product = 15 * 8 * 6 * 13 = 9360
Input: 5 -> 3 -> 4 -> 2 -> 9
Output:
Sum = 6
Product = 8
Explanation:
The list contains 2 Even Digit Sum data values 4 and 2.
Sum = 4 + 2 = 6
Product = 4 * 2 = 8
Approach: The idea is to traverse the given linked list and check whether the sum of all the digits of current node value is even or not. If yes include the current node value to the resultant sum and the resultant product Else check for the next node value.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
};
void push(Node** head_ref, int new_data)
{
Node* new_node
= (Node*) malloc (
sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int digitSum( int num)
{
int sum = 0;
while (num) {
sum += (num % 10);
num /= 10;
}
return sum;
}
void sumAndProduct(Node* head_ref)
{
int prod = 1;
int sum = 0;
Node* ptr = head_ref;
while (ptr != NULL) {
if (!(digitSum(ptr->data) & 1)) {
prod *= ptr->data;
sum += ptr->data;
}
ptr = ptr->next;
}
cout << "Sum = " << sum << endl;
cout << "Product = " << prod;
}
int main()
{
Node* head = NULL;
push(&head, 13);
push(&head, 6);
push(&head, 8);
push(&head, 16);
push(&head, 15);
sumAndProduct(head);
return 0;
}
|
Java
class GFG{
static class Node {
int data;
Node next;
};
static Node push(Node head_ref, int new_data)
{
Node new_node
= new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static int digitSum( int num)
{
int sum = 0 ;
while (num > 0 ) {
sum += (num % 10 );
num /= 10 ;
}
return sum;
}
static void sumAndProduct(Node head_ref)
{
int prod = 1 ;
int sum = 0 ;
Node ptr = head_ref;
while (ptr != null ) {
if ((digitSum(ptr.data) % 2 != 1 )) {
prod *= ptr.data;
sum += ptr.data;
}
ptr = ptr.next;
}
System.out.print( "Sum = " + sum + "\n" );
System.out.print( "Product = " + prod);
}
public static void main(String[] args)
{
Node head = null ;
head = push(head, 13 );
head = push(head, 6 );
head = push(head, 8 );
head = push(head, 16 );
head = push(head, 15 );
sumAndProduct(head);
}
}
|
Python3
class Node:
def __init__( self , x):
self .data = x
self . next = None
def push(head_ref, new_data):
new_node = Node(new_data)
new_node. next = head_ref
head_ref = new_node
return head_ref
def digitSum(num):
sum = 0
while (num):
sum + = (num % 10 )
num / / = 10
return sum
def sumAndProduct(head_ref):
prod = 1
sum = 0
ptr = head_ref
while (ptr ! = None ):
if ( not (digitSum(ptr.data) & 1 )):
prod * = ptr.data
sum + = ptr.data
ptr = ptr. next
print ( "Sum =" , sum )
print ( "Product =" , prod)
if __name__ = = '__main__' :
head = None
head = push(head, 13 )
head = push(head, 6 )
head = push(head, 8 )
head = push(head, 16 )
head = push(head, 15 )
sumAndProduct(head)
|
C#
using System;
class GFG{
class Node
{
public int data;
public Node next;
};
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static int digitSum( int num)
{
int sum = 0;
while (num > 0)
{
sum += (num % 10);
num /= 10;
}
return sum;
}
static void sumAndProduct(Node head_ref)
{
int prod = 1;
int sum = 0;
Node ptr = head_ref;
while (ptr != null )
{
if ((digitSum(ptr.data) % 2 != 1))
{
prod *= ptr.data;
sum += ptr.data;
}
ptr = ptr.next;
}
Console.Write( "Sum = " + sum + "\n" );
Console.Write( "Product = " + prod);
}
public static void Main(String[] args)
{
Node head = null ;
head = push(head, 13);
head = push(head, 6);
head = push(head, 8);
head = push(head, 16);
head = push(head, 15);
sumAndProduct(head);
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
function push(head_ref , new_data) {
var new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
function digitSum(num) {
var sum = 0;
while (num > 0) {
sum += (num % 10);
num = parseInt(num/10);
}
return sum;
}
function sumAndProduct(head_ref) {
var prod = 1;
var sum = 0;
var ptr = head_ref;
while (ptr != null ) {
if ((digitSum(ptr.data) % 2 != 1)) {
prod *= ptr.data;
sum += ptr.data;
}
ptr = ptr.next;
}
document.write( "Sum = " + sum + "<br/>" );
document.write( "Product = " + prod);
}
var head = null ;
head = push(head, 13);
head = push(head, 6);
head = push(head, 8);
head = push(head, 16);
head = push(head, 15);
sumAndProduct(head);
</script>
|
Output
Sum = 42
Product = 9360
Time Complexity: O(N), where N is the number of nodes in the linked list.
Auxiliary Space: O(1)
Recursive Approach:
- Create a recursive function that takes a pointer to the head of the linked list, the sum and the product as arguments.
- If the head pointer is NULL, return the sum and the product.
- Calculate the digit sum of the data of the current node.
- If the digit sum is even, update the sum and the product accordingly.
- Recursively call the function with the next node in the linked list, the updated sum, and the updated product.
- Return the final sum and product.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
};
void push(Node** head_ref, int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int digitSum( int num)
{
int sum = 0;
while (num) {
sum += (num % 10);
num /= 10;
}
return sum;
}
void sumAndProductHelper(Node* ptr, int & prod, int & sum)
{
if (ptr == NULL) {
return ;
}
sumAndProductHelper(ptr->next, prod, sum);
if (!(digitSum(ptr->data) & 1)) {
prod *= ptr->data;
sum += ptr->data;
}
}
void sumAndProduct(Node* head_ref)
{
int prod = 1;
int sum = 0;
sumAndProductHelper(head_ref, prod, sum);
cout << "Sum = " << sum << endl;
cout << "Product = " << prod;
}
int main()
{
Node* head = NULL;
push(&head, 13);
push(&head, 6);
push(&head, 8);
push(&head, 16);
push(&head, 15);
sumAndProduct(head);
return 0;
}
|
Java
class Node {
int data;
Node next;
Node( int data) {
this .data = data;
this .next = null ;
}
}
public class Main {
static Node push(Node head, int newData) {
Node newNode = new Node(newData);
newNode.next = head;
return newNode;
}
static int digitSum( int num) {
int sum = 0 ;
while (num > 0 ) {
sum += num % 10 ;
num /= 10 ;
}
return sum;
}
static void sumAndProductHelper(Node ptr, int [] prodSum) {
if (ptr == null ) {
return ;
}
sumAndProductHelper(ptr.next, prodSum);
if ((digitSum(ptr.data) & 1 ) == 0 ) {
prodSum[ 0 ] *= ptr.data;
prodSum[ 1 ] += ptr.data;
}
}
static void sumAndProduct(Node head) {
int [] prodSum = { 1 , 0 };
sumAndProductHelper(head, prodSum);
System.out.println( "Sum = " + prodSum[ 1 ]);
System.out.println( "Product = " + prodSum[ 0 ]);
}
public static void main(String[] args) {
Node head = null ;
head = push(head, 13 );
head = push(head, 6 );
head = push(head, 8 );
head = push(head, 16 );
head = push(head, 15 );
sumAndProduct(head);
}
}
|
Python
class Node:
def __init__( self , data):
self .data = data
self . next = None
def push(head_ref, new_data):
new_node = Node(new_data)
new_node. next = head_ref[ 0 ]
head_ref[ 0 ] = new_node
def digit_sum(num):
_sum = 0
while num:
_sum + = num % 10
num / / = 10
return _sum
def sum_and_product_helper(ptr, prod, _sum):
if ptr is None :
return
sum_and_product_helper(ptr. next , prod, _sum)
if not digit_sum(ptr.data) & 1 :
prod[ 0 ] * = ptr.data
_sum[ 0 ] + = ptr.data
def sum_and_product(head_ref):
prod = [ 1 ]
_sum = [ 0 ]
sum_and_product_helper(head_ref[ 0 ], prod, _sum)
print ( "Sum =" , _sum[ 0 ])
print ( "Product =" , prod[ 0 ])
if __name__ = = "__main__" :
head = [ None ]
push(head, 13 )
push(head, 6 )
push(head, 8 )
push(head, 16 )
push(head, 15 )
sum_and_product(head)
|
C#
using System;
public class Node
{
public int data;
public Node next;
}
public class GFG
{
public static void Push( ref Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
}
public static int DigitSum( int num)
{
int sum = 0;
while (num != 0)
{
sum += (num % 10);
num /= 10;
}
return sum;
}
public static void SumAndProductHelper(Node ptr, ref int prod, ref int sum)
{
if (ptr == null )
{
return ;
}
SumAndProductHelper(ptr.next, ref prod, ref sum);
if ((DigitSum(ptr.data) & 1) == 0)
{
prod *= ptr.data;
sum += ptr.data;
}
}
public static void SumAndProduct(Node head_ref)
{
int prod = 1;
int sum = 0;
SumAndProductHelper(head_ref, ref prod, ref sum);
Console.WriteLine( "Sum = " + sum);
Console.WriteLine( "Product = " + prod);
}
public static void Main( string [] args)
{
Node head = null ;
Push( ref head, 13);
Push( ref head, 6);
Push( ref head, 8);
Push( ref head, 16);
Push( ref head, 15);
SumAndProduct(head);
}
}
|
Javascript
class Node {
constructor(data) {
this .data = data;
this .next = null ;
}
}
function push(head_ref, new_data) {
let new_node = new Node(new_data);
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
function digitSum(num) {
let sum = 0;
while (num) {
sum += (num % 10);
num = Math.floor(num / 10);
}
return sum;
}
function sumAndProductHelper(ptr, prod, sum) {
if (ptr == null ) {
return ;
}
sumAndProductHelper(ptr.next, prod, sum);
if (!(digitSum(ptr.data) & 1)) {
prod[0] *= ptr.data;
sum[0] += ptr.data;
}
}
function sumAndProduct(head_ref) {
let prod = [1];
let sum = [0];
sumAndProductHelper(head_ref, prod, sum);
console.log( "Sum = " + sum[0]);
console.log( "Product = " + prod[0]);
}
let head = null ;
head = push(head, 13);
head = push(head, 6);
head = push(head, 8);
head = push(head, 16);
head = push(head, 15);
sumAndProduct(head);
|
Output
Sum = 42
Product = 9360
Time Complexity: O(n), where n is the number of nodes in the linked list.
Space Complexity: O(n), where n is the number of nodes in the linked list. This is because for each recursive call.
Last Updated :
19 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...