C# program to check if there are K consecutive 1's in a binary number

A C# program to check if there are K consecutive 1's in a binary number involves counting the longest sequence of consecutive 1's and comparing it with the required count K. This is useful for binary pattern analysis and digital signal processing applications.

Approach

The algorithm iterates through the binary array, maintaining a counter for consecutive 1's. When a 0 is encountered, the counter resets. The maximum consecutive count is tracked using Math.Max() method.

Using Boolean Array Representation

A binary number can be represented as a boolean array where true represents 1 and false represents 0 −

using System;

class BinaryChecker {
   static int CountConsecutiveOnes(bool[] binaryArray) {
      int currentCount = 0;
      int maxCount = 0;
      
      for (int i = 0; i < binaryArray.Length; i++) {
         if (binaryArray[i] == false) {
            currentCount = 0;
         } else {
            currentCount++;
            maxCount = Math.Max(maxCount, currentCount);
         }
      }
      return maxCount;
   }
   
   static bool HasKConsecutiveOnes(bool[] binaryArray, int k) {
      return CountConsecutiveOnes(binaryArray) >= k;
   }
   
   public static void Main() {
      bool[] binaryArray = {false, true, false, false, false, true, true, true};
      int k = 3;
      
      int maxConsecutive = CountConsecutiveOnes(binaryArray);
      Console.WriteLine("Maximum consecutive 1's: " + maxConsecutive);
      Console.WriteLine("Has " + k + " consecutive 1's: " + HasKConsecutiveOnes(binaryArray, k));
   }
}

The output of the above code is −

Maximum consecutive 1's: 3
Has 3 consecutive 1's: True

Using Integer Array Representation

For more intuitive representation, you can use an integer array with 0s and 1s −

using System;

class BinaryAnalyzer {
   static int FindMaxConsecutiveOnes(int[] binary) {
      int maxCount = 0;
      int currentCount = 0;
      
      foreach (int bit in binary) {
         if (bit == 1) {
            currentCount++;
            maxCount = Math.Max(maxCount, currentCount);
         } else {
            currentCount = 0;
         }
      }
      return maxCount;
   }
   
   public static void Main() {
      int[] binaryNumber = {0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1};
      int k = 4;
      
      int result = FindMaxConsecutiveOnes(binaryNumber);
      Console.WriteLine("Binary sequence: " + string.Join("", binaryNumber));
      Console.WriteLine("Longest consecutive 1's: " + result);
      Console.WriteLine("Contains " + k + " consecutive 1's: " + (result >= k));
   }
}

The output of the above code is −

Binary sequence: 011101101111
Longest consecutive 1's: 4
Contains 4 consecutive 1's: True

Using String-Based Binary Input

You can also work with binary strings directly for real-world applications −

using System;

class StringBinaryChecker {
   static int CheckConsecutiveOnes(string binaryString, int k) {
      int maxCount = 0;
      int currentCount = 0;
      
      foreach (char bit in binaryString) {
         if (bit == '1') {
            currentCount++;
            maxCount = Math.Max(maxCount, currentCount);
         } else if (bit == '0') {
            currentCount = 0;
         }
      }
      
      Console.WriteLine("Checking for " + k + " consecutive 1's in: " + binaryString);
      Console.WriteLine("Maximum consecutive 1's found: " + maxCount);
      return maxCount >= k ? maxCount : -1;
   }
   
   public static void Main() {
      string[] testCases = {"101110111", "100011100", "111111000"};
      int k = 3;
      
      foreach (string binary in testCases) {
         int result = CheckConsecutiveOnes(binary, k);
         if (result >= k) {
            Console.WriteLine("Result: Found " + result + " consecutive 1's (? " + k + ")");
         } else {
            Console.WriteLine("Result: No " + k + " consecutive 1's found");
         }
         Console.WriteLine();
      }
   }
}

The output of the above code is −

Checking for 3 consecutive 1's in: 101110111
Maximum consecutive 1's found: 3
Result: Found 3 consecutive 1's (? 3)

Checking for 3 consecutive 1's in: 100011100
Maximum consecutive 1's found: 3
Result: Found 3 consecutive 1's (? 3)

Checking for 3 consecutive 1's in: 111111000
Maximum consecutive 1's found: 6
Result: Found 6 consecutive 1's (? 3)

Key Algorithm Points

  • Reset counter when encountering a 0 (or false)

  • Increment counter when encountering a 1 (or true)

  • Track maximum using Math.Max() to find the longest sequence

  • Compare result with K to determine if the condition is met

Conclusion

Checking for K consecutive 1's in a binary number requires iterating through the binary representation while maintaining counters for current and maximum consecutive sequences. The algorithm efficiently determines whether the binary data contains the required pattern in O(n) time complexity.

Updated on: 2026-03-17T07:04:35+05:30

346 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements