Open In App

Maximum sum of Sublist with composite number nodes in a Linked List

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:

Below is the implementation of the above approach:




// 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;
}

Output
21

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.


Article Tags :