Sum of nodes of Linked List whose contains values with exactly three factors
Last Updated :
21 Jun, 2021
Given a singly linked list containing N nodes, the task is to find the sum of all possible nodes of the list which contains value with exactly three distinct factors.
Examples:
Input: 1 -> 2 -> 4 -> 5
Output: 4
Explanation:
Factors of 2 are {1, 2}
Factors of 3 are {1, 3}
Factors of 4 are {1, 2, 4}
Factors of 5 are {1, 5}
Since only 4 contains three distinct factors. Therefore, the required output is 4.
Input: 1 -> 6 -> 8 -> 10
Output: 0
Approach: The idea is based on the fact that if a number is a prime number, then the square of the number contains exactly three distinct factors. Follow the steps below to solve the problem:
- Initialize a variable, say nodeSum, to store the sum of nodes which contains value with exactly three distinct factors.
- Traverse the linked list and for each node, check if the square root of the current nodes is a prime number or not. If found to be true, then increment nodeSum by the value of current node.
- Finally, print the value of nodeSum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct 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;
}
bool CheckPrimeNumber( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for ( int i = 5; i * i <= n;
i = i + 6) {
if (n % i == 0 || n % (i + 2) == 0)
return false ;
}
return true ;
}
bool checkperfect_square( int n)
{
long double sr = sqrt (n);
if (sr - floor (sr) == 0) {
return true ;
}
return false ;
}
bool is_square( int n)
{
if (checkperfect_square(n)) {
int root = sqrt (n);
if (CheckPrimeNumber(root)) {
return true ;
}
}
return false ;
}
int calculate_sum( struct Node* head)
{
Node* curr = head;
int nodeSum = 0;
while (curr) {
if (is_square(curr->data)) {
nodeSum += curr->data;
}
curr = curr->next;
}
return nodeSum;
}
int main()
{
Node* head = NULL;
push(&head, 5);
push(&head, 4);
push(&head, 2);
push(&head, 1);
cout << calculate_sum(head);
return 0;
}
|
Java
import java.util.*;
class GFG{
static class Node
{
int data;
Node next;
};
static Node head = null ;
static Node push( int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head;
return head = new_node;
}
static boolean CheckPrimeNumber( int n)
{
if (n <= 1 )
return false ;
if (n <= 3 )
return true ;
if (n % 2 == 0 || n % 3 == 0 )
return false ;
for ( int i = 5 ; i * i <= n; i = i + 6 )
{
if (n % i == 0 || n % (i + 2 ) == 0 )
return false ;
}
return true ;
}
static boolean checkperfect_square( int n)
{
long sr = ( long )Math.sqrt(n);
if (sr - Math.floor(sr) == 0 )
{
return true ;
}
return false ;
}
static boolean is_square( int n)
{
if (checkperfect_square(n))
{
int root = ( int )Math.sqrt(n);
if (CheckPrimeNumber(root))
{
return true ;
}
}
return false ;
}
static int calculate_sum(Node head)
{
Node curr = head;
int nodeSum = 0 ;
while (curr.next != null )
{
if (is_square(curr.data))
{
nodeSum += curr.data;
}
curr = curr.next;
}
return nodeSum;
}
public static void main(String[] args)
{
head = push( 5 );
head = push( 4 );
head = push( 2 );
head = push( 1 );
System.out.print(calculate_sum(head));
}
}
|
Python3
import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
class LinkedList:
def __init__( self ):
self .head = None
def Push( self , new_data):
new_node = Node(new_data)
new_node. next = self .head
self .head = new_node
def calculate_sum( self ):
curr = self .head
nodeSum = 0
while (curr):
if (is_square(curr.data)):
nodeSum + = curr.data
curr = curr. next
return nodeSum
def CheckPrimeNumber(n):
if n < = 1 :
return False
if n < = 3 :
return True
if (n % 2 = = 0 or n % 3 = = 0 ):
return False
i = 5
while ((i * i) < = n):
if (n % i = = 0 or n % (i + 2 ) = = 0 ):
return False
i + = 6
return True
def checkperfect_square(n):
sr = math.sqrt(n)
if (sr - math.floor(sr)) = = 0 :
return True
return False
def is_square(n):
if (checkperfect_square(n)):
root = math.sqrt(n)
if (CheckPrimeNumber(root)):
return True
return False
if __name__ = = "__main__" :
LList = LinkedList()
LList.Push( 5 )
LList.Push( 4 )
LList.Push( 2 )
LList.Push( 1 )
print (LList.calculate_sum())
|
C#
using System;
class GFG{
public class Node
{
public int data;
public Node next;
};
static Node head = null ;
static Node push( int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head;
return head = new_node;
}
static bool CheckPrimeNumber( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for ( int i = 5; i * i <= n; i = i + 6)
{
if (n % i == 0 || n % (i + 2) == 0)
return false ;
}
return true ;
}
static bool checkperfect_square( int n)
{
long sr = ( long )Math.Sqrt(n);
if (sr - Math.Floor(( double )sr) == 0)
{
return true ;
}
return false ;
}
static bool is_square( int n)
{
if (checkperfect_square(n))
{
int root = ( int )Math.Sqrt(n);
if (CheckPrimeNumber(root))
{
return true ;
}
}
return false ;
}
static int calculate_sum(Node head)
{
Node curr = head;
int nodeSum = 0;
while (curr.next != null )
{
if (is_square(curr.data))
{
nodeSum += curr.data;
}
curr = curr.next;
}
return nodeSum;
}
public static void Main(String[] args)
{
head = push(5);
head = push(4);
head = push(2);
head = push(1);
Console.Write(calculate_sum(head));
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .data = 0;
this .next = null ;
}
};
var head = null ;
function push(new_data)
{
var new_node = new Node();
new_node.data = new_data;
new_node.next = head;
return head = new_node;
}
function CheckPrimeNumber(n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for ( var i = 5; i * i <= n; i = i + 6)
{
if (n % i == 0 || n % (i + 2) == 0)
return false ;
}
return true ;
}
function checkperfect_square(n)
{
var sr = Math.sqrt(n);
if (sr - Math.floor(sr) == 0)
{
return true ;
}
return false ;
}
function is_square(n)
{
if (checkperfect_square(n))
{
var root = Math.sqrt(n);
if (CheckPrimeNumber(root))
{
return true ;
}
}
return false ;
}
function calculate_sum(head)
{
var curr = head;
var nodeSum = 0;
while (curr.next != null )
{
if (is_square(curr.data))
{
nodeSum += curr.data;
}
curr = curr.next;
}
return nodeSum;
}
head = push(5);
head = push(4);
head = push(2);
head = push(1);
document.write(calculate_sum(head));
</script>
|
Time Complexity: O(N * √ X), where X is the largest element in the linked list
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...