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++ code for the above approach: #include <bits/stdc++.h> using namespace std;
// Defining the structure of a node // in a binary linked list struct Node {
int data;
Node* next;
}; // Function to create a new node Node* newNode( int data)
{ Node* node = new Node();
node->data = data;
node->next = NULL;
return node;
} // Function to determine whether the // given binary linked list contains a // contiguous sequence of 1s of length K bool containsContiguousSequence(Node* head, int K)
{ // Initialize a counter for consecutive 1s
int count = 0;
// Initialize a flag to indicate if
// the contiguous sequence is found
bool flag = false ;
while (head) {
// Increment the count if the current
// node contains 1
if (head->data == 1) {
count++;
if (count == K) {
// Set the flag to true if the
// contiguous sequence of
// length K is found
flag = true ;
// Exit the loop early since the
// sequence has been found
break ;
}
}
// Reset the count if the current
// node contains 0
else {
count = 0;
}
// Move to the next node in the
// linked list
head = head->next;
}
// Return true if a contiguous sequence of
// 1s of length K was found, otherwise
// false
return flag;
} // Driver code int main()
{ // Creating a sample binary linked list
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);
// Printing the result
if (result) {
cout << "True";
}
else {
cout << "False";
}
return 0;
} |
// Defining the structure of a node in a binary linked list class Node {
int data;
Node next;
Node( int data) {
this .data = data;
this .next = null ;
}
} public class Main {
// Function to determine whether the given binary linked list contains
// a contiguous sequence of 1s of length K
public static boolean containsContiguousSequence(Node head, int K) {
// Initialize a counter for consecutive 1s
int count = 0 ;
// Initialize a flag to indicate if the contiguous sequence is found
boolean flag = false ;
while (head != null ) {
// Increment the count if the current node contains 1
if (head.data == 1 ) {
count++;
if (count == K) {
// Set the flag to true if the contiguous sequence of length K is found
flag = true ;
// Exit the loop early since the sequence has been found
break ;
}
} else {
// Reset the count if the current node contains 0
count = 0 ;
}
// Move to the next node in the linked list
head = head.next;
}
// Return true if a contiguous sequence of 1s of length K was found, otherwise false
return flag;
}
public static void main(String[] args) {
// Creating a sample binary linked list
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 );
// Printing the result
if (result) {
System.out.println( "True" );
} else {
System.out.println( "False" );
}
}
} //Contributed by Aditi Tyagi |
class Node:
def __init__( self , data):
self .data = data
self . next = None
def contains_contiguous_sequence(head, K):
# Initialize a counter for consecutive 1s
count = 0
# Initialize a flag to indicate if the contiguous sequence is found
flag = False
while head:
# Increment the count if the current node contains 1
if head.data = = 1 :
count + = 1
if count = = K:
# Set the flag to true if the contiguous sequence of
# length K is found
flag = True
# Exit the loop early since the sequence has been found
break
else :
# Reset the count if the current node contains 0
count = 0
# Move to the next node in the linked list
head = head. next
# Return True if a contiguous sequence of 1s of length K was found, otherwise False
return flag
# Driver code if __name__ = = "__main__" :
# Creating a sample binary linked list
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 )
# Printing the result
if result:
print ( "True" )
else :
print ( "False" )
|
// C# Code using System;
// Defining the structure of a node in a binary linked list public class Node
{ public int Data { get ; set ; }
public Node Next { get ; set ; }
public Node( int data)
{
Data = data;
Next = null ;
}
} public class GFG
{ // Function to determine whether the given binary linked list contains
// a contiguous sequence of 1s of length K
public static bool ContainsContiguousSequence(Node head, int K)
{
// Initialize a counter for consecutive 1s
int count = 0;
// Initialize a flag to indicate if the contiguous sequence is found
bool flag = false ;
while (head != null )
{
// Increment the count if the current node contains 1
if (head.Data == 1)
{
count++;
if (count == K)
{
// Set the flag to true if the contiguous sequence of length K is found
flag = true ;
// Exit the loop early since the sequence has been found
break ;
}
}
else
{
// Reset the count if the current node contains 0
count = 0;
}
// Move to the next node in the linked list
head = head.Next;
}
// Return true if a contiguous sequence of 1s of length K was found, otherwise false
return flag;
}
public static void Main()
{
// Creating a sample binary linked list
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);
// Printing the result
if (result)
{
Console.WriteLine( "True" );
}
else
{
Console.WriteLine( "False" );
}
}
} // This code is contributed by guptapratik |
// Definition of a node in a binary linked list class Node { constructor(data) {
this .data = data;
this .next = null ;
}
} // Function to determine whether the given binary linked list contains a contiguous sequence of 1s of length K function containsContiguousSequence(head, K) {
// Initialize a counter for consecutive 1s
let count = 0;
// Initialize a flag to indicate if the contiguous sequence is found
let flag = false ;
while (head) {
// Increment the count if the current node contains 1
if (head.data === 1) {
count++;
if (count === K) {
// Set the flag to true if the contiguous sequence of length K is found
flag = true ;
// Exit the loop early since the sequence has been found
break ;
}
} else {
// Reset the count if the current node contains 0
count = 0;
}
// Move to the next node in the linked list
head = head.next;
}
// Return true if a contiguous sequence of 1s of length K was found, otherwise false
return flag;
} // Main function function main() {
// Creating a sample binary linked list
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);
// Printing the result
console.log(result ? "True" : "False" );
} // Call the main function main(); |
True
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.