Maximum sum of Sublist with composite number nodes in a Linked List
Last Updated :
25 Apr, 2023
Given a linked list, the task is to find the maximum sum of a sublist with composite number nodes. A composite number is any positive integer greater than 1 that is not a prime number.
Examples:
Input: List: 1 -> 4 -> 8 -> 7 -> 6 -> 6 -> 9
Output: 21
Explanation: The sublist with composite number nodes is 6->6->9 and the maximum of this sublist is 21.
Input: List: 8 -> 7 -> 11 -> 2 -> 6 -> 4 -> 3
Output: 10
Explanation: The sublist with composite number nodes is 6->4 and the maximum of this sublist is 10.
Approach: This can be solved with the following idea:
To solve the problem, we can traverse the linked list and maintain a variable to store the maximum sum of the sublist with composite number nodes found so far. We can also maintain another variable to store the sum of the current sublist. We can use a helper function to check whether a given number is composite or not. To check whether a number is composite, we can iterate from 2 to the square root of the number and check if any of the numbers divide the given number evenly.
Below is the step-by-step approach:
- Initialize a variable max_sum to zero to store the maximum sum of the sublist with composite number nodes found so far.
- Initialize a variable current_sum to zero to store the sum of the current sublist.
- Traverse the linked list.
- If the current node’s value is composite, add it to the current_sum variable.
- If the current node’s value is not composite or if it is the last node in the linked list, check if the current_sum variable is greater than max_sum. If it is, update the max_sum variable.
- If the current node’s value is not composite, reset the current_sum variable to zero.
- Return the max_sum variable.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
};
bool isComposite( int n)
{
if (n <= 1) {
return false ;
}
for ( int i = 2; i <= sqrt (n); i++) {
if (n % i == 0) {
return true ;
}
}
return false ;
}
int maxSumOfCompositeNodes(Node* head)
{
int max_sum = 0;
int current_sum = 0;
while (head != NULL) {
if (isComposite(head->data)) {
current_sum += head->data;
}
else {
if (current_sum > max_sum) {
max_sum = current_sum;
}
current_sum = 0;
}
head = head->next;
}
if (current_sum > max_sum) {
max_sum = current_sum;
}
return max_sum;
}
Node* newNode( int data)
{
Node* node = new Node;
node->data = data;
node->next = NULL;
return node;
}
int main()
{
Node* head = newNode(1);
head->next = newNode(4);
head->next->next = newNode(8);
head->next->next->next = newNode(7);
head->next->next->next->next = newNode(6);
head->next->next->next->next->next = newNode(6);
head->next->next->next->next->next->next = newNode(9);
int max_sum = maxSumOfCompositeNodes(head);
cout << max_sum << endl;
return 0;
}
|
Time Complexity: O(n*sqrt(n)), where n is the number of nodes in the linked list.
Auxiliary Space: O(1), as we are not using any additional data structures to store the linked list.