Maximum sum of Sublist with composite number nodes in a Linked List Last Updated : 25 Apr, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 -> 9Output: 21Explanation: 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 -> 3Output: 10Explanation: 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++ // C++ Implementation #include <bits/stdc++.h> using namespace std; // Node of the linked list struct Node { int data; Node* next; }; // Function to check whether a number is // composite or not 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; } // Function to find the maximum sum of a // sublist with composite number nodes 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; } // Function to create a new node // in the linked list Node* newNode(int data) { Node* node = new Node; node->data = data; node->next = NULL; return node; } // Driver code int main() { // Creating the linked list 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); // Function call int max_sum = maxSumOfCompositeNodes(head); cout << max_sum << endl; return 0; } Output21 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. Comment More infoAdvertise with us Next Article Minimum and Maximum Prime Numbers of a Singly Linked List V vishaldhaygude01 Follow Improve Article Tags : Linked List DSA Linked Lists Practice Tags : Linked List Similar Reads Maximum sum contiguous nodes in the given linked list Given a linked list, the task is to find the maximum sum for any contiguous nodes. Examples: Input: -2 -> -3 -> 4 -> -1 -> -2 -> 1 -> 5 -> -3 -> NULL Output: 7 4 -> -1 -> -2 -> 1 -> 5 is the sub-list with the given sum. Input: 1 -> 2 -> 3 -> 4 -> NULL 11 min read Maximum sum of K consecutive nodes in the given Linked List Given a linked list, the task is to find the maximum sum obtained by adding any k consecutive nodes of linked list. Examples: Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULL, K = 5 Output: 20 Maximum sum is obtained by adding last 5 nodes Input: 2 -> 5 -> 3 -> 6 -> 4 -> 1 8 min read Minimum and Maximum Prime Numbers of a Singly Linked List Given a singly linked list containing N nodes, the task is to find the minimum and maximum prime number. Examples: Input : List = 15 -> 16 -> 6 -> 7 -> 17 Output : Minimum : 7 Maximum : 17 Input : List = 15 -> 3 -> 4 -> 2 -> 9 Output : Minimum : 2 Maximum : 3 Approach: The id 9 min read Replace every node with closest Prime number in a Singly Linked List Given a linked list, the task is to replace every node with its closest prime number and return the modified list. Examples: Input: List: 1 -> 2 -> 8 -> 7 -> 14 -> NULLOutput: 2 -> 2 -> 7 -> 7 -> 13 -> NULLExplanation: For the first node 1, the closest prime number is 2 12 min read Find product of nodes with min and max weights in a singly linked list Given a singly linked list with weights, the task is to find the product of nodes with minimum and maximum weights in the list. Examples: Input: 3 (4) -> 1 (2) -> 8 (14) -> 12 (1) -> 5 (9) -> 7 (3) -> NULLOutput: 96Explanation: Node with minimum weight is 12 and maximum weight is 8 7 min read Maximum XOR sublist of length K in Linked list Given a linked list of integers and an integer value K, the task is to find the maximum XOR value of a sublist of length K in the linked list. Examples: Input: 4 -> 6 -> 9 -> 7, K = 2Output: 15Explanation: All possible sublists of length K = 2 starting from each node.4 -> 6, 6 -> 9, 9 10 min read Like