Product of Smallest and Largest prime numbers in a Linked List
Last Updated :
12 Oct, 2023
INTRODUCTION:-
A linked list is a data structure that consists of a sequence of elements called nodes. Each node in the linked list contains a data element and a reference to the next node in the sequence. Linked lists are commonly used in computer programming for a variety of applications, including the implementation of dynamic data structures, such as stacks, queues, and hash tables.
In this article, we will discuss how to find the product of the smallest and largest prime numbers in a linked list.
APPROACH:-
The problem can be solved by iterating through the linked list and identifying the prime numbers. Once the prime numbers have been identified, the smallest and largest prime numbers can be determined, and their product can be calculated.
To solve this problem, we will need to define two helper functions: isPrime and getMaxNode.
The isPrime function takes an integer as input and returns true if the number is prime and false otherwise. The function works by checking whether the input number is divisible by any number between 2 and the square root of the input number.
The getMaxNode function takes a linked list as input and returns the node with the largest data element. The function works by iterating through the linked list and keeping track of the node with the largest data element.
With these helper functions in place, we can now write the main function to solve the problem. The function will take a linked list as input and return the product of the smallest and largest prime numbers in the list.
ALGORITHM:-
Here is the algorithm for the main function:
- Initialize a variable smallestPrime to infinity and largestPrime to zero.
- Initialize a variable currentNode to the head of the linked list.
- Iterate through the linked list.
- For each node, check if the data element is prime using the isPrime function.
- If the data element is prime, check if it is smaller than smallestPrime or larger than largestPrime. If so, update smallestPrime or largestPrime accordingly.
- Set currentNode to the next node in the linked list.
- Once the iteration is complete, return the product of smallestPrime and largestPrime.
Given a Linked list of integers, find the product of the smallest and largest prime numbers present in the list.
Input: 11 -> 8 -> 15 -> 3 -> 10 -> 21
Output: 33
Explanation: The smallest prime number is 3 and the largest prime number is 11. The product of 3 and 11 is 33.
Input: 1 -> 7 -> 6 -> 8 -> 9 -> 5
Output: 35
Explanation: The smallest prime number is 5 and the largest prime number is 7. The product of 5 and 7 is 35.
EXAMPLE:-
Lets look on the example of an input:11->8->15->3->10->21.
In C++, JAVA, PYTHON AND C#.
Below is the code for the above approach:
C++
// C++ Implementation
#include <iostream>
using namespace std;
// Linked list node
struct Node {
int data;
struct Node* next;
Node(int x)
{
data = x;
next = NULL;
}
};
// Function to check whether a number is
// prime or not
bool isPrime(int n)
{
if (n <= 1)
return false;
for (int i = 2; i * i <= n; i++)
if (n % i == 0)
return false;
return true;
}
// Function to find the product of
// smallest and largest prime numbers
// in a linked list
int productOfSmallestAndLargestPrimes(Node* head)
{
int smallest = -1, largest = -1;
while (head != NULL) {
if (isPrime(head->data)) {
if (smallest == -1 || head->data < smallest) {
smallest = head->data;
}
if (largest == -1 || head->data > largest) {
largest = head->data;
}
}
head = head->next;
}
if (smallest == -1 || largest == -1) {
return -1;
}
return smallest * largest;
}
// Driver code
int main()
{
Node* head = new Node(11);
head->next = new Node(8);
head->next->next = new Node(15);
head->next->next->next = new Node(3);
head->next->next->next->next = new Node(10);
head->next->next->next->next->next = new Node(21);
// Function call
cout << productOfSmallestAndLargestPrimes(head) << endl;
return 0;
}
Java
public class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
Node head;
public LinkedList() {
this.head = null;
}
public void addNode(int data) {
Node newNode = new Node(data);
if (this.head == null) {
this.head = newNode;
} else {
Node currentNode = this.head;
while (currentNode.next != null) {
currentNode = currentNode.next;
}
currentNode.next = newNode;
}
}
}
class Main {
public static boolean isPrime(int n) {
if (n < 2) {
return false;
}
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
public static int productOfSmallestAndLargestPrime(LinkedList linkedList) {
int smallestPrime = Integer.MAX_VALUE;
int largestPrime = 0;
Node currentNode = linkedList.head;
while (currentNode != null) {
if (isPrime(currentNode.data)) {
if (currentNode.data < smallestPrime) {
smallestPrime = currentNode.data;
}
if (currentNode.data > largestPrime) {
largestPrime = currentNode.data;
}
}
currentNode = currentNode.next;
}
return smallestPrime * largestPrime;
}
public static void main(String[] args) {
LinkedList linkedList = new LinkedList();
linkedList.addNode(11);
linkedList.addNode(8);
linkedList.addNode(15);
linkedList.addNode(3);
linkedList.addNode(10);
linkedList.addNode(21);
int product = productOfSmallestAndLargestPrime(linkedList);
System.out.println(product); //output=33
}
}
Python
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def addNode(self, data):
newNode = Node(data)
if self.head is None:
self.head = newNode
else:
currentNode = self.head
while currentNode.next is not None:
currentNode = currentNode.next
currentNode.next = newNode
def isPrime(n):
if n < 2:
return False
for i in range(2, int(n**0.5)+1):
if n % i == 0:
return False
return True
def productOfSmallestAndLargestPrime(linkedList):
smallestPrime = float('inf')
largestPrime = 0
currentNode = linkedList.head
while currentNode is not None:
if isPrime(currentNode.data):
if currentNode.data < smallestPrime:
smallestPrime = currentNode.data
if currentNode.data > largestPrime:
largestPrime = currentNode.data
currentNode = currentNode.next
return smallestPrime * largestPrime
linkedList = LinkedList()
linkedList.addNode(11)
linkedList.addNode(8)
linkedList.addNode(15)
linkedList.addNode(3)
linkedList.addNode(10)
linkedList.addNode(21)
product = productOfSmallestAndLargestPrime(linkedList)
print(product)
C#
// C# Implementation
using System;
// Linked list node
public class Node {
public int data;
public Node next;
public Node(int x)
{
data = x;
next = null;
}
}
public class GFG {
// Function to check whether a number is prime or not
public static bool IsPrime(int n)
{
if (n <= 1)
return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
// Function to find the product of smallest and largest
// prime numbers in a linked list
public static int
ProductOfSmallestAndLargestPrimes(Node head)
{
int smallest = -1, largest = -1;
while (head != null) {
if (IsPrime(head.data)) {
if (smallest == -1
|| head.data < smallest) {
smallest = head.data;
}
if (largest == -1 || head.data > largest) {
largest = head.data;
}
}
head = head.next;
}
if (smallest == -1 || largest == -1) {
return -1;
}
return smallest * largest;
}
// Driver code
public static void Main(string[] args)
{
Node head = new Node(11);
head.next = new Node(8);
head.next.next = new Node(15);
head.next.next.next = new Node(3);
head.next.next.next.next = new Node(10);
head.next.next.next.next.next = new Node(21);
// Function call
Console.WriteLine(
ProductOfSmallestAndLargestPrimes(head));
}
}
// This code is contributed by Susobhan Akhuli
JavaScript
<script>
// JavaScript Implementation
// Linked list node
class Node {
constructor(x) {
this.data = x;
this.next = null;
}
}
// Function to check whether a number is prime or not
function isPrime(n) {
if (n <= 1) {
return false;
}
for (let i = 2; i * i <= n; i++) {
if (n % i === 0) {
return false;
}
}
return true;
}
// Function to find the product of smallest and largest prime numbers in a linked list
function productOfSmallestAndLargestPrimes(head) {
let smallest = -1;
let largest = -1;
let current = head;
while (current !== null) {
if (isPrime(current.data)) {
if (smallest === -1 || current.data < smallest) {
smallest = current.data;
}
if (largest === -1 || current.data > largest) {
largest = current.data;
}
}
current = current.next;
}
if (smallest === -1 || largest === -1) {
return -1;
}
return smallest * largest;
}
const head = new Node(11);
head.next = new Node(8);
head.next.next = new Node(15);
head.next.next.next = new Node(3);
head.next.next.next.next = new Node(10);
head.next.next.next.next.next = new Node(21);
// Function call
document.write(productOfSmallestAndLargestPrimes(head));
// This code is contributed by Susobhan Akhuli
</script>
TIME COMPLEXITY:-
It is important to note that the time complexity of this algorithm is O(n*sqrt(n)), where n is the number of nodes in the linked list. This is because we need to iterate through each node in the linked list and check whether it is prime, which takes O(sqrt(n)) time for each node.
Time Complexity: O(n*sqrt(n)), where n is the length of the linked list
Auxiliary Space: O(1)
CONCLUSION:-
In conclusion, we have discussed how to find the product of the smallest and largest prime numbers in a linked list. This problem can be solved by iterating through the linked list and identifying the prime numbers. Once the prime numbers have been identified, the smallest and largest prime numbers can be determined, and their product can be calculated. By defining two helper functions and using them in the main function, we can solve this problem efficiently.
Similar Reads
Find product of the smallest and largest prime numbers in a binary tree
Given a binary tree, the task is to find the product of the smallest and largest prime numbers in a given binary tree.Examples:Input: 4 / \ 5 7 / \ / \ 1 3 5 8Output: 21Input: 6 / \ 8 11 / \ / \ 4 3 9 15Output: 33Approach: This can be solved with the following idea:The main idea behind this approach
14 min read
Sum and product of k smallest and k largest prime numbers in the array
Given an integer k and an array of integers arr, the task is to find the sum and product of k smallest and k largest prime numbers in the array. Assume that there are at least k prime numbers in the array. Examples: Input: arr[] = {2, 5, 6, 8, 10, 11}, k = 2 Output: Sum of k-minimum prime numbers is
15+ min read
Product of all prime nodes in a Doubly Linked List
Given a doubly linked list containing N nodes. The task is to find the product of all prime nodes. Example: Input: List = 15 <=> 16 <=> 6 <=> 7 <=> 17 Output: Product of Prime Nodes: 119 Input: List = 5 <=> 3 <=> 4 <=> 2 <=> 9 Output: Product of Prime
10 min read
Smallest and Largest Prime Numbers
Prime numbers are natural numbers greater than 1 that have no divisors other than 1 and themselves. The first few prime numbers are:2, 3, 5, 7, 11, 13, 17, 19, 23, 29, . . .Here, we can see the smallest prime number is 2, as there is no smaller prime than this. But, there is no largest prime number,
2 min read
Sum and Product of all Prime 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 nodes from the list which are prime. Examples: Input : List = 15 -> 16 -> 6 -> 7 -> 17 Output : Product = 119, Sum = 24 Prime nodes are 7, 17. Input : List = 15 -> 3 -> 4 -> 2 -> 9 O
15+ min read
Find the Product of first N Prime Numbers
Given a positive integer N, calculate the product of the first N prime numbers. Examples: Input : N = 3 Output : 30 Explanation : First 3 prime numbers are 2, 3, 5. Input : N = 5 Output : 2310 Approach: Create a sieve which will help us to identify if the number is prime or not in O(1) time. Run a l
7 min read
Find the Pair of Nodes with the Smallest Product in a Doubly Linked List
Given a doubly linked list, the task is to find the pair of nodes whose product is the smallest among all possible pairs. Each node in the list contains an integer value, and you need to find the two nodes whose multiplication results in the minimum product. Examples: Input: 4 <-> 6 <->
11 min read
Find the product of last N nodes of the given Linked List
Given a linked list and a number N. Find the product of last n nodes of the linked list. Constraints : 0 <= N <= number of nodes in the linked list. Examples: Input : List = 10->6->8->4->12, N = 2 Output : 48 Explanation : Product of last two nodes: 12 * 4 = 48 Input : List = 15-
15+ min read
Product of all prime numbers in an Array
Given an array arr[] of N positive integers. The task is to write a program to find the product of all the prime numbers of the given array. Examples: Input: arr[] = {1, 3, 4, 5, 7} Output: 105 There are three primes, 3, 5 and 7 whose product = 105. Input: arr[] = {1, 2, 3, 4, 5, 6, 7} Output: 210 N
11 min read
Find the Max product Sublist of length K in a Linked list
Given a linked list of integers and a number K, find the maximum product of a sublist of length K in a linked list. Examples: Input: List: 1 -> -2 -> -3 -> 4 -> -5, K = 3Output: 60Explanation: The product of sublist -3 -> 4 -> -5 is 60 which is the maximum product of a sublist of l
13 min read