
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
C# program to check if there are K consecutive 1’s in a binary number
To check for consecutive 1’s in a binary number, you need to check for 0 and 1.
Firstly, set a bool array for 0s and 1s i.e. false and true −
bool []myArr = {false, true, false, false, false, true, true, true};
For 0, set the count to 0 −
if (myArr[i] == false) count = 0;
For 1, increment the count and set the result. The Max() method returns the larger of two number −
count++; res = Math.Max(res, count);
Example
The following is the example to check if there are K consecutive 1’s in a binary number −
using System; class MyApplication { static int count(bool []myArr, int num) { int myCount = 0, res = 0; for (int i = 0; i < num; i++) { if (myArr[i] == false) myCount = 0; else { myCount++; res = Math.Max(res, myCount); } } return res; } public static void Main() { bool []myArr = {false, true, false, false, false, true, true, true}; int num = myArr.Length; Console.Write("Consecutive 1's = "+count(myArr, num)); } }
Output
Consecutive 1's = 3
- Related Articles
- Python program to check if there are K consecutive 1’s in a binary number?
- C/C++ Program to Count number of binary strings without consecutive 1’s?
- Program to Count number of binary strings without consecutive 1’s in C/C++?
- Count number of binary strings without consecutive 1's in C
- Program to find longest consecutive run of 1 in binary form of a number in C++
- Finding maximum number of consecutive 1's in a binary array in JavaScript
- Check If All 1's Are at Least Length K Places Away in C++
- Python Program to Count number of binary strings without consecutive 1’
- Check if a binary string contains consecutive same or not in C++
- C# program to find the length of the Longest Consecutive 1’s in Binary Representation of a given integer
- Count Binary String without Consecutive 1's
- Check if a binary string has two consecutive occurrences of one everywhere in C++
- Binary representation of next greater number with same number of 1’s and 0’s in C Program?
- Check if a binary string contains all permutations of length k in C++
- Check If a String Contains All Binary Codes of Size K in C++

Advertisements