# Maximum sum of K consecutive nodes in the given Linked List

Given a linked list, the task is tofind 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 -> 7 -> NULL, K = 4
Output: 18

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The idea is to use a sliding window of size k, keep track of sum of current window and update maximum sum if required. To implement sliding window two pointers can be used to represent starting and ending point. At each step first the value of node pointed by start is subtracted from current sum and the value of node pointed by end is added to current sum. This sum is compared to maximum sum and result is updated if required. The start and end pointers are incremented by one at each step.

Below is the implementation of above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Structure of a node ` `struct` `Node { ` `    ``int` `data; ` `    ``Node* next; ` `}; ` ` `  `// Function to create new node ` `Node* newNode(``int` `data) ` `{ ` `    ``Node* node = ``new` `Node; ` `    ``node->next = NULL; ` `    ``node->data = data; ` `    ``return` `node; ` `} ` ` `  `// Function to return the maximum sum of ` `// k consecutive nodes ` `int` `findMaxSum(Node* head, ``int` `k) ` `{ ` `    ``// To store current window sum ` `    ``int` `sum = 0; ` ` `  `    ``// To store maximum sum ` `    ``int` `maxSum = 0; ` ` `  `    ``// Pointer to the start of window ` `    ``Node* start = head; ` ` `  `    ``// Pointer to the end of window ` `    ``Node* end = head; ` ` `  `    ``int` `i; ` ` `  `    ``// Find the sum of first k nodes ` `    ``for` `(i = 0; i < k; i++) { ` `        ``sum += end->data; ` `        ``end = end->next; ` `    ``} ` ` `  `    ``maxSum = sum; ` ` `  `    ``// Move window by one step and ` `    ``// update sum. Node pointed by ` `    ``// start is excluded from current ` `    ``// window so subtract it. Node ` `    ``// pointed by end is added to ` `    ``// current window so add its value. ` `    ``while` `(end != NULL) { ` ` `  `        ``// Subtract the starting element ` `        ``// from previous window ` `        ``sum -= start->data; ` `        ``start = start->next; ` ` `  `        ``// Add the element next to the end ` `        ``// of previous window ` `        ``sum += end->data; ` `        ``end = end->next; ` ` `  `        ``// Update the maximum sum so far ` `        ``maxSum = max(maxSum, sum); ` `    ``} ` ` `  `    ``return` `maxSum; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``Node* head = newNode(1); ` `    ``head->next = newNode(2); ` `    ``head->next->next = newNode(3); ` `    ``head->next->next->next = newNode(4); ` `    ``head->next->next->next->next = newNode(5); ` `    ``head->next->next->next->next->next = newNode(6); ` ` `  `    ``int` `k = 5; ` ` `  `    ``cout << findMaxSum(head, k); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `class` `GFG ` `{ ` ` `  `// Structure of a node ` `static` `class` `Node ` `{ ` `    ``int` `data; ` `    ``Node next; ` `}; ` ` `  `// Function to create new node ` `static` `Node newNode(``int` `data) ` `{ ` `    ``Node node = ``new` `Node(); ` `    ``node.next = ``null``; ` `    ``node.data = data; ` `    ``return` `node; ` `} ` ` `  `// Function to return the maximum sum of ` `// k consecutive nodes ` `static` `int` `findMaxSum(Node head, ``int` `k) ` `{ ` `    ``// To store current window sum ` `    ``int` `sum = ``0``; ` ` `  `    ``// To store maximum sum ` `    ``int` `maxSum = ``0``; ` ` `  `    ``// Pointer to the start of window ` `    ``Node start = head; ` ` `  `    ``// Pointer to the end of window ` `    ``Node end = head; ` ` `  `    ``int` `i; ` ` `  `    ``// Find the sum of first k nodes ` `    ``for` `(i = ``0``; i < k; i++)  ` `    ``{ ` `        ``sum += end.data; ` `        ``end = end.next; ` `    ``} ` ` `  `    ``maxSum = sum; ` ` `  `    ``// Move window by one step and ` `    ``// update sum. Node pointed by ` `    ``// start is excluded from current ` `    ``// window so subtract it. Node ` `    ``// pointed by end is added to ` `    ``// current window so add its value. ` `    ``while` `(end != ``null``) ` `    ``{ ` ` `  `        ``// Subtract the starting element ` `        ``// from previous window ` `        ``sum -= start.data; ` `        ``start = start.next; ` ` `  `        ``// Add the element next to the end ` `        ``// of previous window ` `        ``sum += end.data; ` `        ``end = end.next; ` ` `  `        ``// Update the maximum sum so far ` `        ``maxSum = Math.max(maxSum, sum); ` `    ``} ` `    ``return` `maxSum; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``Node head = newNode(``1``); ` `    ``head.next = newNode(``2``); ` `    ``head.next.next = newNode(``3``); ` `    ``head.next.next.next = newNode(``4``); ` `    ``head.next.next.next.next = newNode(``5``); ` `    ``head.next.next.next.next.next = newNode(``6``); ` ` `  `    ``int` `k = ``5``; ` `    ``System.out.print(findMaxSum(head, k)); ` `} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

## C#

 `// C# implementation of the approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` ` `  `// Structure of a node ` `public` `class` `Node ` `{ ` `    ``public` `int` `data; ` `    ``public` `Node next; ` `}; ` ` `  `// Function to create new node ` `static` `Node newNode(``int` `data) ` `{ ` `    ``Node node = ``new` `Node(); ` `    ``node.next = ``null``; ` `    ``node.data = data; ` `    ``return` `node; ` `} ` ` `  `// Function to return the maximum sum of ` `// k consecutive nodes ` `static` `int` `findMaxSum(Node head, ``int` `k) ` `{ ` `    ``// To store current window sum ` `    ``int` `sum = 0; ` ` `  `    ``// To store maximum sum ` `    ``int` `maxSum = 0; ` ` `  `    ``// Pointer to the start of window ` `    ``Node start = head; ` ` `  `    ``// Pointer to the end of window ` `    ``Node end = head; ` ` `  `    ``int` `i; ` ` `  `    ``// Find the sum of first k nodes ` `    ``for` `(i = 0; i < k; i++)  ` `    ``{ ` `        ``sum += end.data; ` `        ``end = end.next; ` `    ``} ` ` `  `    ``maxSum = sum; ` ` `  `    ``// Move window by one step and ` `    ``// update sum. Node pointed by ` `    ``// start is excluded from current ` `    ``// window so subtract it. Node ` `    ``// pointed by end is added to ` `    ``// current window so add its value. ` `    ``while` `(end != ``null``) ` `    ``{ ` ` `  `        ``// Subtract the starting element ` `        ``// from previous window ` `        ``sum -= start.data; ` `        ``start = start.next; ` ` `  `        ``// Add the element next to the end ` `        ``// of previous window ` `        ``sum += end.data; ` `        ``end = end.next; ` ` `  `        ``// Update the maximum sum so far ` `        ``maxSum = Math.Max(maxSum, sum); ` `    ``} ` `    ``return` `maxSum; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``Node head = newNode(1); ` `    ``head.next = newNode(2); ` `    ``head.next.next = newNode(3); ` `    ``head.next.next.next = newNode(4); ` `    ``head.next.next.next.next = newNode(5); ` `    ``head.next.next.next.next.next = newNode(6); ` ` `  `    ``int` `k = 5; ` `    ``Console.Write(findMaxSum(head, k)); ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

Output:

```20
```

Time Complexity: O(n)
Auxiliary Space: O(1)

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : princiraj1992, Rajput-Ji