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 -> 7 -> NULL, K = 4
Output: 18
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++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* newNode( int data)
{
Node* node = new Node;
node->next = NULL;
node->data = data;
return node;
}
int findMaxSum(Node* head, int k)
{
int sum = 0;
int maxSum = 0;
Node* start = head;
Node* end = head;
int i;
for (i = 0; i < k; i++) {
sum += end->data;
end = end->next;
}
maxSum = sum;
while (end != NULL) {
sum -= start->data;
start = start->next;
sum += end->data;
end = end->next;
maxSum = max(maxSum, sum);
}
return maxSum;
}
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
class GFG
{
static class Node
{
int data;
Node next;
};
static Node newNode( int data)
{
Node node = new Node();
node.next = null ;
node.data = data;
return node;
}
static int findMaxSum(Node head, int k)
{
int sum = 0 ;
int maxSum = 0 ;
Node start = head;
Node end = head;
int i;
for (i = 0 ; i < k; i++)
{
sum += end.data;
end = end.next;
}
maxSum = sum;
while (end != null )
{
sum -= start.data;
start = start.next;
sum += end.data;
end = end.next;
maxSum = Math.max(maxSum, sum);
}
return maxSum;
}
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));
}
}
|
Python3
class Node:
def __init__( self , x):
self .data = x
self . next = None
def findMaxSum(head, k):
sum = 0
maxSum = 0
start = head
end = head
for i in range (k):
sum + = end.data
end = end. next
maxSum = sum
while (end ! = None ):
sum - = start.data
start = start. next
sum + = end.data
end = end. next
maxSum = max (maxSum, sum )
return maxSum
if __name__ = = '__main__' :
head = Node( 1 )
head. next = Node( 2 )
head. next . next = Node( 3 )
head. next . next . next = Node( 4 )
head. next . next . next . next = Node( 5 )
head. next . next . next . next . next = Node( 6 )
k = 5
print (findMaxSum(head, k))
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node newNode( int data)
{
Node node = new Node();
node.next = null ;
node.data = data;
return node;
}
static int findMaxSum(Node head, int k)
{
int sum = 0;
int maxSum = 0;
Node start = head;
Node end = head;
int i;
for (i = 0; i < k; i++)
{
sum += end.data;
end = end.next;
}
maxSum = sum;
while (end != null )
{
sum -= start.data;
start = start.next;
sum += end.data;
end = end.next;
maxSum = Math.Max(maxSum, sum);
}
return maxSum;
}
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));
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
function newNode(data) {
var node = new Node();
node.next = null ;
node.data = data;
return node;
}
function findMaxSum(head , k) {
var sum = 0;
var maxSum = 0;
var start = head;
var end = head;
var i;
for (i = 0; i < k; i++) {
sum += end.data;
end = end.next;
}
maxSum = sum;
while (end != null ) {
sum -= start.data;
start = start.next;
sum += end.data;
end = end.next;
maxSum = Math.max(maxSum, sum);
}
return maxSum;
}
var 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);
var k = 5;
document.write(findMaxSum(head, k));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
02 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...