Product of all nodes in a doubly linked list divisible by a given number K
Given a doubly-linked list containing N nodes and given a number K. The task is to find the product of all such nodes which are divisible by K.
Examples:
Input : List = 15 <=> 16 <=> 10 <=> 9 <=> 6 <=> 7 <=> 17
K = 3
Output : Product = 810
Input : List = 5 <=> 3 <=> 6 <=> 8 <=> 4 <=> 1 <=> 2 <=> 9
K = 2
Output : Product = 384
The idea is to traverse the doubly linked list and check the nodes one by one. If a node’s value is divisible by K then multiply that node’s value with the product so far and continue this process while the end of the list is not reached.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
struct Node {
int data;
Node *prev, *next;
};
void push(Node** head_ref, int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->prev = NULL;
new_node->next = (*head_ref);
if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;
(*head_ref) = new_node;
}
int productOfNode(Node** head_ref, int K)
{
Node* ptr = *head_ref;
Node* next;
int product = 1;
while (ptr != NULL) {
next = ptr->next;
if (ptr->data % K == 0)
product *= ptr->data;
ptr = next;
}
return product;
}
int main()
{
Node* head = NULL;
push(&head, 17);
push(&head, 7);
push(&head, 6);
push(&head, 9);
push(&head, 10);
push(&head, 16);
push(&head, 15);
int K = 3;
int prod = productOfNode(&head, K);
cout << "Product = " << prod;
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node prev, next;
};
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.prev = null ;
new_node.next = (head_ref);
if ((head_ref) != null )
(head_ref).prev = new_node;
(head_ref) = new_node;
return head_ref;
}
static int productOfNode(Node head_ref, int K)
{
Node ptr = head_ref;
Node next;
int product = 1 ;
while (ptr != null )
{
next = ptr.next;
if (ptr.data % K == 0 )
product *= ptr.data;
ptr = next;
}
return product;
}
public static void main(String args[])
{
Node head = null ;
head = push(head, 17 );
head = push(head, 7 );
head = push(head, 6 );
head = push(head, 9 );
head = push(head, 10 );
head = push(head, 16 );
head = push(head, 15 );
int K = 3 ;
int prod = productOfNode(head, K);
System.out.println( "Product = " + prod);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .prev = None
self . next = None
def push(head_ref, new_data):
new_node = Node( 0 )
new_node.data = new_data
new_node.prev = None
new_node. next = (head_ref)
if ((head_ref) ! = None ):
(head_ref).prev = new_node
(head_ref) = new_node
return head_ref
def productOfNode(head_ref, K):
ptr = head_ref
next = None
product = 1
while (ptr ! = None ) :
next = ptr. next
if (ptr.data % K = = 0 ):
product * = ptr.data
ptr = next
return product
if __name__ = = "__main__" :
head = None
head = push(head, 17 )
head = push(head, 7 )
head = push(head, 6 )
head = push(head, 9 )
head = push(head, 10 )
head = push(head, 16 )
head = push(head, 15 )
K = 3
product = productOfNode(head, K)
print ( "product =" , product)
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node prev, next;
};
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.prev = null ;
new_node.next = (head_ref);
if ((head_ref) != null )
(head_ref).prev = new_node;
(head_ref) = new_node;
return head_ref;
}
static int productOfNode(Node head_ref, int K)
{
Node ptr = head_ref;
Node next;
int product = 1;
while (ptr != null )
{
next = ptr.next;
if (ptr.data % K == 0)
product *= ptr.data;
ptr = next;
}
return product;
}
public static void Main(String []args)
{
Node head = null ;
head = push(head, 17);
head = push(head, 7);
head = push(head, 6);
head = push(head, 9);
head = push(head, 10);
head = push(head, 16);
head = push(head, 15);
int K = 3;
int prod = productOfNode(head, K);
Console.WriteLine( "Product = " + prod);
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .prev = null ;
this .next = null ;
}
}
function push(head_ref , new_data) {
var new_node = new Node();
new_node.data = new_data;
new_node.prev = null ;
new_node.next = (head_ref);
if ((head_ref) != null )
(head_ref).prev = new_node;
(head_ref) = new_node;
return head_ref;
}
function productOfNode(head_ref , K) {
var ptr = head_ref;
var next;
var product = 1;
while (ptr != null ) {
next = ptr.next;
if (ptr.data % K == 0)
product *= ptr.data;
ptr = next;
}
return product;
}
var head = null ;
head = push(head, 17);
head = push(head, 7);
head = push(head, 6);
head = push(head, 9);
head = push(head, 10);
head = push(head, 16);
head = push(head, 15);
var K = 3;
var prod = productOfNode(head, K);
document.write( "Product = " + prod);
</script>
|
Complexity Analysis:
- Time Complexity: O(N), where N is the number of nodes.
- Auxiliary Space: O(1)
Last Updated :
27 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...