Sudo Placement[1.4] | K Sum
Last Updated :
29 Aug, 2022
Given head of a linked list of integers and an integer k, your task is to modify the linked list as follows:
- Consider nodes in groups of size k. In every group, replace value of first node with group sum.
- Also, delete the elements of group except the first node.
- During traversal, if the remaining nodes in linked list are less than k then also do the above considering remaining nodes.
Examples:
Input: N = 6, K = 2
1->2->3->4->5->6
Output: 3 7 11
We have denoted grouping of k elements by (). The elements inside () are summed.
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null
(1 -> 2) -> 3 -> 4 -> 5 -> 6 -> null
(3) -> 3 -> 4 -> 5 -> 6 -> null
3 -> (3 -> 4) -> 5 -> 6 -> null
3 -> (7) -> 5 -> 6 -> null
3 -> 7 -> (5 -> 6) -> null
3 -> 7 -> (11) -> null
3 -> 7 -> 11 -> null
Approach:
Insert the given nodes in the Linked list. The approach of insertion has been discussed in this post. Once the nodes are inserted, iterate from the beginning of the list. Mark the first node as temp node. Iterate for the next k-1 nodes and sum up the nodes in sum variable. If the number of nodes is less than K, then replace the temp node’s data with sum and point temp to NULL.
If there is a group with K nodes, replace the temp’s data with sum and move temp to the node which is just after K nodes. Continue the above operation till temp points to NULL. Once temp reaches the end, it means all groups of size K has been traversed and the node has been replaced with the sum of nodes of size K. Once the operation of replacements have been done, the linked list thus formed can be printed. The method of printing the linked list has been discussed in this post.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
struct Node {
long long data;
Node* next;
};
Node* newNode( int key)
{
Node* temp = new Node;
temp->data = key;
temp->next = NULL;
return temp;
}
Node* insertNode(Node* head, int key)
{
if (head == NULL)
head = newNode(key);
else
head->next = insertNode(head->next, key);
return head;
}
void print(Node* head)
{
while (head) {
cout << head->data << " " ;
head = head->next;
}
cout << endl;
}
void KSum(Node* head, int k)
{
Node* temp = head;
while (temp) {
long long count = k;
long long sum = 0;
Node* curr = temp;
while (count-- && curr) {
sum = sum + curr->data;
curr = curr->next;
}
temp->data = sum;
temp->next = curr;
temp = temp->next;
}
}
int main()
{
Node* head = NULL;
head = insertNode(head, 1);
head = insertNode(head, 2);
head = insertNode(head, 3);
head = insertNode(head, 4);
head = insertNode(head, 5);
head = insertNode(head, 6);
int k = 2;
KSum(head, k);
print(head);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class Node
{
int data;
Node next;
Node( int key)
{
data = key;
next = null ;
}
}
class GFG
{
static Node head;
public static Node insertNode(Node head, int key)
{
if (head == null )
head = new Node(key);
else
head.next = insertNode(head.next, key);
return head;
}
public static void print(Node head)
{
while (head != null )
{
System.out.print(head.data + " " );
head = head.next;
}
System.out.println();
}
public static void KSum(Node head, int k)
{
Node temp = head;
while (temp != null )
{
int count = k;
int sum = 0 ;
Node curr = temp;
while (count > 0 && curr != null )
{
sum = sum + curr.data;
curr = curr.next;
count--;
}
temp.data = sum;
temp.next = curr;
temp = temp.next;
}
}
public static void main(String args[])
{
head = null ;
head = insertNode(head, 1 );
head = insertNode(head, 2 );
head = insertNode(head, 3 );
head = insertNode(head, 4 );
head = insertNode(head, 5 );
head = insertNode(head, 6 );
int k = 2 ;
KSum(head, k);
print(head);
}
}
|
Python3
class Node:
def __init__( self , key):
self .data = key
self . next = None
def insertNode(head: Node, key: int ) - > Node:
if head is None :
head = Node(key)
else :
head. next = insertNode(head. next , key)
return head
def Print (head: Node):
while head:
print (head.data, end = " " )
head = head. next
print ()
def KSum(head: Node, k: int ):
temp = head
while temp:
count = k
sum = 0
curr = temp
while count and curr:
sum = sum + curr.data
curr = curr. next
count - = 1
temp.data = sum
temp. next = curr
temp = temp. next
if __name__ = = "__main__" :
head = None
head = insertNode(head, 1 )
head = insertNode(head, 2 )
head = insertNode(head, 3 )
head = insertNode(head, 4 )
head = insertNode(head, 5 )
head = insertNode(head, 6 )
k = 2
KSum(head, k)
Print (head)
|
C#
using System;
public class Node
{
public int data;
public Node next;
public Node( int key)
{
data = key;
next = null ;
}
}
public class GFG
{
static Node head;
public static Node insertNode(Node head, int key)
{
if (head == null )
head = new Node(key);
else
head.next = insertNode(head.next, key);
return head;
}
public static void print(Node head)
{
while (head != null )
{
Console.Write(head.data + " " );
head = head.next;
}
Console.WriteLine();
}
public static void KSum(Node head, int k)
{
Node temp = head;
while (temp != null )
{
int count = k;
int sum = 0;
Node curr = temp;
while (count > 0 && curr != null )
{
sum = sum + curr.data;
curr = curr.next;
count--;
}
temp.data = sum;
temp.next = curr;
temp = temp.next;
}
}
public static void Main()
{
head = null ;
head = insertNode(head, 1);
head = insertNode(head, 2);
head = insertNode(head, 3);
head = insertNode(head, 4);
head = insertNode(head, 5);
head = insertNode(head, 6);
int k = 2;
KSum(head, k);
print(head);
}
}
|
Javascript
<script>
class Node
{
constructor(key)
{
this .data = key;
this .next = null ;
}
}
var head = null ;
function insertNode(head, key)
{
if (head == null ) head = new Node(key);
else head.next = insertNode(head.next, key);
return head;
}
function print(head)
{
while (head != null )
{
document.write(head.data + " " );
head = head.next;
}
document.write( "<br>" );
}
function KSum(head, k)
{
var temp = head;
while (temp != null )
{
var count = k;
var sum = 0;
var curr = temp;
while (count > 0 && curr != null )
{
sum = sum + curr.data;
curr = curr.next;
count--;
}
temp.data = sum;
temp.next = curr;
temp = temp.next;
}
}
head = null ;
head = insertNode(head, 1);
head = insertNode(head, 2);
head = insertNode(head, 3);
head = insertNode(head, 4);
head = insertNode(head, 5);
head = insertNode(head, 6);
var k = 2;
KSum(head, k);
print(head);
</script>
|
Share your thoughts in the comments
Please Login to comment...