Longest Continuous Sequence with difference K in Linked List
Last Updated :
11 Oct, 2023
Given a linked list and given an integer K, the task is to find the length of the longest continuous sequence in a linked list that has a difference of k.
Examples:
Input: 4 -> 1 -> 8 -> 5 -> 2 -> 4, K = 3
Output: 3
Explanation: The longest continuous sequence with a difference of 3 in linked list is 8 -> 5 -> 2 has a difference of 3. Therefore, the function should return 3.
Input: 12 -> 3 -> 7 -> 1, K = 5
Output: 0
Explanation: There is no continuous sequence of list with a difference of 5 in linked list. Hence, the function should return 0.
Approach: To solve the problem follow the below idea:
The idea behind the approach is to use a Hashset to keep track of the values encountered in the Linked List.
Follow the steps to solve the problem:
- For each node, check if the current node value + K exists in the hash set. If it does, we enter a loop to increment the length of the continuous sequence. Start with a length of 1 and a current value of the current node value.
- Inside the loop, keep incrementing the current value by K and check if the updated value exists in the hash set. If it does, increment the length and update the current value.
- After the loop, compare the length with maxLength and update maxLength if the length is greater.
- Repeat steps 3-5 for checking if the current node value – K exists in the hash set, and if it does, enter a similar loop to find the length of the continuous sequence in the decreasing direction.
- Finally, insert the current node value into the hash set and move to the next node in the linked list.
- Once have traversed the entire linked list, return the maxLength as the length of the longest continuous sequence.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
};
void insert(Node** head, int data)
{
Node* newNode = new Node();
newNode->data = data;
newNode->next = *head;
*head = newNode;
}
int findLongestContinuousSequence(Node* head, int k)
{
unordered_set< int > hashSet;
int maxLength = 0;
while (head != nullptr) {
if (hashSet.find(head->data + k) != hashSet.end()) {
int length = 1;
int curr = head->data;
while (hashSet.find(curr + k)
!= hashSet.end()) {
length++;
curr += k;
}
maxLength = std::max(maxLength, length);
}
if (hashSet.find(head->data - k) != hashSet.end()) {
int length = 1;
int curr = head->data;
while (hashSet.find(curr - k)
!= hashSet.end()) {
length++;
curr -= k;
}
maxLength = max(maxLength, length);
}
hashSet.insert(head->data);
head = head->next;
}
return maxLength;
}
int main()
{
Node* head = nullptr;
insert(&head, 4);
insert(&head, 3);
insert(&head, 8);
insert(&head, 5);
insert(&head, 2);
insert(&head, 4);
int k = 3;
int maxLength = findLongestContinuousSequence(head, k);
cout << "Length of the longest continuous sequence: "
<< maxLength << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.HashSet;
class Node {
int data;
Node next;
}
public class Main {
static void insert(Node[] head, int data) {
Node newNode = new Node();
newNode.data = data;
newNode.next = head[ 0 ];
head[ 0 ] = newNode;
}
static int findLongestContinuousSequence(Node head, int k) {
HashSet<Integer> hashSet = new HashSet<>();
int maxLength = 0 ;
while (head != null ) {
if (hashSet.contains(head.data + k)) {
int length = 1 ;
int curr = head.data;
while (hashSet.contains(curr + k)) {
length++;
curr += k;
}
maxLength = Math.max(maxLength, length);
}
if (hashSet.contains(head.data - k)) {
int length = 1 ;
int curr = head.data;
while (hashSet.contains(curr - k)) {
length++;
curr -= k;
}
maxLength = Math.max(maxLength, length);
}
hashSet.add(head.data);
head = head.next;
}
return maxLength;
}
public static void main(String[] args) {
Node[] head = new Node[ 1 ];
insert(head, 4 );
insert(head, 3 );
insert(head, 8 );
insert(head, 5 );
insert(head, 2 );
insert(head, 4 );
int k = 3 ;
int maxLength = findLongestContinuousSequence(head[ 0 ], k);
System.out.println( "Length of the longest continuous sequence: " + maxLength);
}
}
|
Python
class Node:
def __init__( self , data):
self .data = data
self . next = None
def insert(head, data):
new_node = Node(data)
new_node. next = head
head = new_node
return head
def find_longest_continuous_sequence(head, k):
hash_set = set ()
max_length = 0
while head is not None :
if head.data + k in hash_set:
length = 1
curr = head.data
while curr + k in hash_set:
length + = 1
curr + = k
max_length = max (max_length, length)
if head.data - k in hash_set:
length = 1
curr = head.data
while curr - k in hash_set:
length + = 1
curr - = k
max_length = max (max_length, length)
hash_set.add(head.data)
head = head. next
return max_length
head = None
head = insert(head, 4 )
head = insert(head, 3 )
head = insert(head, 8 )
head = insert(head, 5 )
head = insert(head, 2 )
head = insert(head, 4 )
k = 3
max_length = find_longest_continuous_sequence(head, k)
print ( "Length of the longest continuous sequence:" , max_length)
|
C#
using System;
using System.Collections.Generic;
class Node {
public int data;
public Node next;
}
public class MainClass {
static void Insert(Node[] head, int data)
{
Node newNode = new Node();
newNode.data = data;
newNode.next = head[0];
head[0] = newNode;
}
static int FindLongestContinuousSequence(Node head,
int k)
{
HashSet< int > hashSet = new HashSet< int >();
int maxLength = 0;
while (head != null ) {
if (hashSet.Contains(head.data + k)) {
int length = 1;
int curr = head.data;
while (hashSet.Contains(curr + k)) {
length++;
curr += k;
}
maxLength = Math.Max(maxLength, length);
}
if (hashSet.Contains(head.data - k)) {
int length = 1;
int curr = head.data;
while (hashSet.Contains(curr - k)) {
length++;
curr -= k;
}
maxLength = Math.Max(maxLength, length);
}
hashSet.Add(head.data);
head = head.next;
}
return maxLength;
}
public static void Main( string [] args)
{
Node[] head = new Node[1];
Insert(head, 4);
Insert(head, 3);
Insert(head, 8);
Insert(head, 5);
Insert(head, 2);
Insert(head, 4);
int k = 3;
int maxLength
= FindLongestContinuousSequence(head[0], k);
Console.WriteLine(
"Length of the longest continuous sequence: "
+ maxLength);
}
}
|
Javascript
class Node {
constructor(data) {
this .data = data;
this .next = null ;
}
}
function insert(head, data) {
const newNode = new Node(data);
newNode.next = head;
return newNode;
}
function findLongestContinuousSequence(head, k) {
const hashSet = new Set();
let maxLength = 0;
while (head !== null ) {
if (hashSet.has(head.data + k)) {
let length = 1;
let curr = head.data;
while (hashSet.has(curr + k)) {
length++;
curr += k;
}
maxLength = Math.max(maxLength, length);
}
if (hashSet.has(head.data - k)) {
let length = 1;
let curr = head.data;
while (hashSet.has(curr - k)) {
length++;
curr -= k;
}
maxLength = Math.max(maxLength, length);
}
hashSet.add(head.data);
head = head.next;
}
return maxLength;
}
let head = null ;
head = insert(head, 4);
head = insert(head, 3);
head = insert(head, 8);
head = insert(head, 5);
head = insert(head, 2);
head = insert(head, 4);
const k = 3;
const maxLength = findLongestContinuousSequence(head, k);
console.log( "Length of the longest continuous sequence: " , maxLength);
|
Output
Length of the longest continuous sequence: 3
Time Complexity: O(N), where N is the number of nodes in the linked list.
Auxiliary Space: O(N), where N is the number of nodes in the linked list.
Share your thoughts in the comments
Please Login to comment...