Check Contiguous 1s Sequence in Binary Linked List
Last Updated :
03 Dec, 2023
Given a binary linked list and integer K, the task is to check whether the binary linked list contains a contiguous sequence of 1s of K length.
Examples:
Input: 0 -> 1 -> 1 -> 1 -> 0 -> 1 -> 1 -> 0 -> 1 -> 1, K = 3
Output: True
Explanation: In the given binary linked list, there is a contiguous sequence of three 1s between the second and fourth nodes (1 -> 1 -> 1). Therefore, the output is return True.
Input: 0 -> 0 -> 1 -> 0 -> 1 -> 1 -> 1 -> 1 -> 0, K = 4
Output: True
Explanation: In the given binary linked list, there is a contiguous sequence of four 1s between the fifth and eight nodes (1 -> 1 -> 1). Therefore, the output is return True.
Input: 0 -> 1 -> 0 -> 0-> 0-> 1, K = 5
Output: False
Explanation: In the given binary linked list, there is no contiguous sequence of five 1s so the output is False.
Approach: To solve the problem follow the below idea:
To solve this problem idea is to iterating through the linked list and keeping track of a counter variable count that increments whenever a 1 is encountered and resets to 0 whenever a 0 is encountered.
Here is steps of above approach:
- Initialize variable count to 0.
- Traverse the linked list using a while loop. Inside the loop, we check whether the current node contains a 1 or a 0.
- If the current node contains a 1, we increment a counter variable count by 1.
- If the value of count becomes equal to K, it means we have found a contiguous sequence of 1s of length K, so we set the Boolean flag variable flag to true and break out of the loop.
- the current node contains a 0, we reset the value of count to 0, since a contiguous sequence of 1s has been broken.
- Finally, we return the value of flag, which indicates whether we found a contiguous sequence of 1s of length K in the linked list.
Below is the implementation of the 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->data = data;
node->next = NULL;
return node;
}
bool containsContiguousSequence(Node* head, int K)
{
int count = 0;
bool flag = false ;
while (head) {
if (head->data == 1) {
count++;
if (count == K) {
flag = true ;
break ;
}
}
else {
count = 0;
}
head = head->next;
}
return flag;
}
int main()
{
Node* head = newNode(0);
head->next = newNode(1);
head->next->next = newNode(1);
head->next->next->next = newNode(1);
head->next->next->next->next = newNode(0);
head->next->next->next->next->next = newNode(1);
head->next->next->next->next->next->next = newNode(1);
head->next->next->next->next->next->next->next
= newNode(0);
head->next->next->next->next->next->next->next->next
= newNode(1);
head->next->next->next->next->next->next->next->next
->next
= newNode(1);
bool result = containsContiguousSequence(head, 3);
if (result) {
cout << "True";
}
else {
cout << "False";
}
return 0;
}
|
Java
class Node {
int data;
Node next;
Node( int data) {
this .data = data;
this .next = null ;
}
}
public class Main {
public static boolean containsContiguousSequence(Node head, int K) {
int count = 0 ;
boolean flag = false ;
while (head != null ) {
if (head.data == 1 ) {
count++;
if (count == K) {
flag = true ;
break ;
}
} else {
count = 0 ;
}
head = head.next;
}
return flag;
}
public static void main(String[] args) {
Node head = new Node( 0 );
head.next = new Node( 1 );
head.next.next = new Node( 1 );
head.next.next.next = new Node( 1 );
head.next.next.next.next = new Node( 0 );
head.next.next.next.next.next = new Node( 1 );
head.next.next.next.next.next.next = new Node( 1 );
head.next.next.next.next.next.next.next = new Node( 0 );
head.next.next.next.next.next.next.next.next = new Node( 1 );
head.next.next.next.next.next.next.next.next.next = new Node( 1 );
boolean result = containsContiguousSequence(head, 3 );
if (result) {
System.out.println( "True" );
} else {
System.out.println( "False" );
}
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def contains_contiguous_sequence(head, K):
count = 0
flag = False
while head:
if head.data = = 1 :
count + = 1
if count = = K:
flag = True
break
else :
count = 0
head = head. next
return flag
if __name__ = = "__main__" :
head = Node( 0 )
head. next = Node( 1 )
head. next . next = Node( 1 )
head. next . next . next = Node( 1 )
head. next . next . next . next = Node( 0 )
head. next . next . next . next . next = Node( 1 )
head. next . next . next . next . next . next = Node( 1 )
head. next . next . next . next . next . next . next = Node( 0 )
head. next . next . next . next . next . next . next . next = Node( 1 )
head. next . next . next . next . next . next . next . next . next = Node( 1 )
result = contains_contiguous_sequence(head, 3 )
if result:
print ( "True" )
else :
print ( "False" )
|
C#
using System;
public class Node
{
public int Data { get ; set ; }
public Node Next { get ; set ; }
public Node( int data)
{
Data = data;
Next = null ;
}
}
public class GFG
{
public static bool ContainsContiguousSequence(Node head, int K)
{
int count = 0;
bool flag = false ;
while (head != null )
{
if (head.Data == 1)
{
count++;
if (count == K)
{
flag = true ;
break ;
}
}
else
{
count = 0;
}
head = head.Next;
}
return flag;
}
public static void Main()
{
Node head = new Node(0);
head.Next = new Node(1);
head.Next.Next = new Node(1);
head.Next.Next.Next = new Node(1);
head.Next.Next.Next.Next = new Node(0);
head.Next.Next.Next.Next.Next = new Node(1);
head.Next.Next.Next.Next.Next.Next = new Node(1);
head.Next.Next.Next.Next.Next.Next.Next = new Node(0);
head.Next.Next.Next.Next.Next.Next.Next.Next = new Node(1);
head.Next.Next.Next.Next.Next.Next.Next.Next.Next = new Node(1);
bool result = ContainsContiguousSequence(head, 3);
if (result)
{
Console.WriteLine( "True" );
}
else
{
Console.WriteLine( "False" );
}
}
}
|
Javascript
class Node {
constructor(data) {
this .data = data;
this .next = null ;
}
}
function containsContiguousSequence(head, K) {
let count = 0;
let flag = false ;
while (head) {
if (head.data === 1) {
count++;
if (count === K) {
flag = true ;
break ;
}
} else {
count = 0;
}
head = head.next;
}
return flag;
}
function main() {
const head = new Node(0);
head.next = new Node(1);
head.next.next = new Node(1);
head.next.next.next = new Node(1);
head.next.next.next.next = new Node(0);
head.next.next.next.next.next = new Node(1);
head.next.next.next.next.next.next = new Node(1);
head.next.next.next.next.next.next.next = new Node(0);
head.next.next.next.next.next.next.next.next = new Node(1);
head.next.next.next.next.next.next.next.next.next = new Node(1);
const result = containsContiguousSequence(head, 3);
console.log(result ? "True" : "False" );
}
main();
|
Time Complexity: O(n), where n is the number of nodes in the linked list.
Auxiliary Space: O(1), since we are only using a constant amount of extra space to store the counter variable count and the Boolean flag variable flag.
Share your thoughts in the comments
Please Login to comment...