Given a number N. Find the length of the longest consecutive 1s in its binary representation.
Example 1:
Input: N = 14
Output: 3
Explanation: 
Binary representation of 14 is 
1110, in which 111 is the longest 
consecutive set bits of length is 3.
Example 2:
Input: N = 222
Output: 4
Explanation: 
Binary representation of 222 is 
11011110, in which 1111 is the 
longest consecutive set bits of length 4. 
Your Task: 
You don't need to read input or print anything. Your task is to complete the function maxConsecutiveOnes() which returns the length of the longest consecutive 1s in the binary representation of given N.
Expected Time Complexity: O(log N).
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 10^{6}

Source: geeksforgeeks | Difficulty: EASY
Tags: Bit manipulation

Data structures

Starter code: #User function Template for python3





class Solution:

    ##Complete this function

    # Function to calculate the longest consecutive ones

    def maxConsecutiveOnes(self, N):

        ##Your code here







Solutions:
class Solution:

	def maxConsecutiveOnes(self, n):
		maxi = 0
		modcount = 0
		while n:
			mod = n % 2
			if not mod:
				modcount = 0
			if mod:
				modcount += 1
			n = n // 2
			maxi = max(maxi, modcount)
		return maxi


class Solution:

	def maxConsecutiveOnes(self, N):
		binary = bin(N)[2:]
		current_length = 0
		max_length = 0
		for bit in binary:
			if bit == '1':
				current_length += 1
				max_length = max(current_length, max_length)
			else:
				current_length = 0
		return max_length


import math

class Solution:

	def maxConsecutiveOnes(self, N):
		count = 0
		maxi = -math.inf
		while N:
			if N & 1 == 1:
				count += 1
			else:
				maxi = max(count, maxi)
				count = 0
			N >>= 1
		return max(maxi, count)


class Solution:

	def maxConsecutiveOnes(self, N):
		a = bin(N)[2:]
		lst = a.split('0')
		m = 0
		for i in lst:
			m = max(m, len(i))
		return m


class Solution:

	def maxConsecutiveOnes(self, n):
		m = c = 0
		while n > 0:
			if n & 1 == 0:
				c = 0
			else:
				c += 1
				m = max(m, c)
			n = n >> 1
		return m


class Solution:

	def maxConsecutiveOnes(self, N):
		count = 0
		while N != 0:
			N = N & N << 1
			count += 1
		return count


class Solution:

	def maxConsecutiveOnes(self, N):
		resultado = 0
		unos = 0
		binario = bin(N)[2:]
		for l in binario:
			if l == '1':
				unos += 1
			else:
				if unos > resultado:
					resultado = unos
				unos = 0
		if unos > resultado:
			return unos
		return resultado


class Solution:

	def maxConsecutiveOnes(self, N):
		count = 0
		max1 = 0
		while N != 0:
			if N & 1 == 1:
				count += 1
				max1 = max(count, max1)
			else:
				count = 0
			N = N // 2
		return max1


class Solution:

	def maxConsecutiveOnes(self, N):
		c = 0
		while N != 0:
			N = N & N << 1
			c += 1
		return c


class Solution:

	def maxConsecutiveOnes(self, n):
		c = 0
		while n:
			n = n & n << 1
			c += 1
		return c


class Solution:

	def maxConsecutiveOnes(self, n):
		a = bin(n)[2:].split('0')
		res = 0
		for i in a:
			res = max(res, len(i))
		return res


class Solution:

	def maxConsecutiveOnes(self, N):
		binn = bin(N)[2:]
		c = 1
		l = []
		for i in range(len(binn) - 1):
			if binn[i] == '1' and binn[i + 1] == '1':
				c += 1
			else:
				l.append(c)
				c = 1
		l.append(c)
		return max(l)


class Solution:

	def maxConsecutiveOnes(self, N):
		x = bin(N)
		x = str(x)
		msf = 0
		c = 0
		for i in range(2, len(x)):
			if x[i] == '1':
				c += 1
			else:
				msf = max(msf, c)
				c = 0
			msf = max(msf, c)
		return msf


class Solution:

	def maxConsecutiveOnes(self, N):
		n = str(N)
		a = bin(N).replace('0b', '')
		s = str(a)
		(count, maxi) = (0, 0)
		for i in range(len(s)):
			if s[i] == '1':
				count += 1
				maxi = max(maxi, count)
			else:
				count = 0
		return maxi


class Solution:

	def maxConsecutiveOnes(self, N):
		(max_res, res) = (0, 0)
		while N > 0:
			if N & 1:
				res += 1
			else:
				(max_res, res) = (max(max_res, res), 0)
			N = N >> 1
		return max(max_res, res)


class Solution:

	def maxConsecutiveOnes(self, N):
		x = list(bin(N)[2:])
		c = 0
		m = 0
		for i in x:
			if i == '1':
				c += 1
				m = max(c, m)
			else:
				c = 0
		return m


class Solution:

	def maxConsecutiveOnes(self, N):
		N = bin(N)[2:]
		M = N.split('0')
		return len(max(M))


class Solution:

	def maxConsecutiveOnes(self, N):
		binary = bin(N)[2:]
		count = 0
		max_count = 0
		for i in range(len(binary)):
			if binary[i] == '1':
				count += 1
			else:
				max_count = max(max_count, count)
				count = 0
		max_count = max(max_count, count)
		return max_count


class Solution:

	def maxConsecutiveOnes(self, N):
		c = 0
		m = N
		lar = 0
		while m != 0:
			if m & 1 == 1:
				c = c + 1
			else:
				c = 0
			if c > lar:
				lar = c
			m = m >> 1
		return lar


class Solution:

	def maxConsecutiveOnes(self, N):
		a = bin(N)[2:]
		l = len(a)
		while l != 0:
			if '1' * l in a:
				return l
			else:
				l -= 1


class Solution:

	def maxConsecutiveOnes(self, x):
		count = 0
		while x != 0:
			x = x & x << 1
			count = count + 1
		return count


class Solution:

	def maxConsecutiveOnes(self, N):
		from collections import Counter
		x = bin(N)[2:]
		maxx = 0
		c = 0
		for i in x:
			if i == '1':
				c = c + 1
				maxx = max(maxx, c)
			else:
				c = 0
		return maxx


class Solution:

	def maxConsecutiveOnes(self, N):
		r = 0
		t = 0
		while N >= 1:
			if N % 2:
				t += 1
			else:
				t = 0
			N //= 2
			if r < t:
				r = t
		if r < t:
			r = t
		return r


class Solution:

	def maxConsecutiveOnes(self, N):
		c = ''
		temp = N
		while temp:
			if temp % 2 == 0:
				c += '0'
				temp = temp // 2
			else:
				c += '1'
				temp = temp // 2
		d = c[::-1]
		count = 0
		res = 0
		for i in d:
			if i == '1':
				count += 1
				res = max(res, count)
			else:
				count = 0
		return res


class Solution:

	def maxConsecutiveOnes(self, N):
		res = 0
		tmp = 0
		while N >= 1:
			if N % 2:
				tmp += 1
			else:
				tmp = 0
			N //= 2
			if res < tmp:
				res = tmp
		if res < tmp:
			res = tmp
		return res


class Solution:

	def maxConsecutiveOnes(self, N):
		ans = 0
		temp = 0
		while N:
			rem = N % 2
			if rem == 1:
				temp += 1
			else:
				ans = max(ans, temp)
				temp = 0
			N = N // 2
		ans = max(ans, temp)
		return ans


class Solution:

	def maxConsecutiveOnes(self, N):
		maxi = 0
		cnt = 0
		while N:
			ans = N % 2
			if ans == 0:
				cnt = 0
			else:
				cnt += 1
			maxi = max(maxi, cnt)
			N >>= 1
		return maxi


class Solution:

	def maxConsecutiveOnes(self, N):
		count = 0
		best = 0
		while N > 0:
			if N & 1:
				count += 1
			else:
				if count > best:
					best = count
				count = 0
			N >>= 1
		if count > best:
			best = count
		return best


class Solution:

	def maxConsecutiveOnes(self, N):
		m = 0
		c = 0
		for i in str(bin(N)):
			if i == '1':
				c += 1
			else:
				m = max(m, c)
				c = 0
		m = max(m, c)
		return m


class Solution:

	def maxConsecutiveOnes(self, N):
		r = 0
		s = 0
		while N > 0:
			if N & 1 == 1:
				s += 1
				r = max(r, s)
			else:
				s = 0
			N >>= 1
		return r


class Solution:

	def maxConsecutiveOnes(self, N):
		b = bin(N)[2:]
		result = 0
		count = 0
		for i in b:
			if i == '1':
				count += 1
			else:
				result = max(result, count)
				count = 0
		result = max(result, count)
		return result


class Solution:

	def maxConsecutiveOnes(self, N):
		b = bin(N)
		c = b.replace('b', '0')[2:]
		arr = list(c)
		ans = []
		c = 0
		for i in arr:
			if i == '1':
				c += 1
				ans.append(c)
			else:
				c = 0
				ans.append(c)
		return max(ans)


class Solution:

	def maxConsecutiveOnes(self, N):
		max_length = 0
		count = 0
		bit_check_num = 1
		while bit_check_num <= N:
			if bit_check_num & N:
				count += 1
				max_length = max(max_length, count)
			else:
				count = 0
			bit_check_num = bit_check_num << 1
		return max_length


class Solution:

	def maxConsecutiveOnes(self, N):
		c = 0
		mx = 0
		while N:
			if N & 1:
				c += 1
			else:
				c = 0
			N = N >> 1
			mx = max(c, mx)
		return mx


class Solution:

	def maxConsecutiveOnes(self, N):
		b = bin(N)[2:] + '0'
		c = 0
		m = -1000000000.0
		for i in range(len(b)):
			if b[i] == '1':
				c = c + 1
			else:
				m = max(m, c)
				c = 0
		return m


class Solution:

	def maxConsecutiveOnes(self, N):
		x = bin(N)[2:]
		(count, amx) = (0, 0)
		for i in x:
			if i == '1':
				count += 1
			else:
				amx = max(amx, count)
				count = 0
		return max(amx, count)


import re

class Solution:

	def maxConsecutiveOnes(self, N):
		x = bin(N)
		m = re.findall('1+', x)
		return len(max(m))


class Solution:

	def maxConsecutiveOnes(self, N):
		maximum = 0
		count = 0
		while N:
			if N & 1:
				count += 1
			else:
				count = 0
			maximum = max(maximum, count)
			N >>= 1
		return maximum


class Solution:

	def maxConsecutiveOnes(self, N):
		res = bin(N)
		count = 0
		max = 0
		for i in res:
			if i == '1':
				count = count + 1
				if max < count:
					max = count
			else:
				count = 0
		return max


class Solution:

	def maxConsecutiveOnes(self, N):
		binnum = bin(N)
		binNumber = binnum[2:]
		max1 = 0
		counter = 0
		for i in binNumber:
			if i == '1':
				counter += 1
			else:
				max1 = max(max1, counter)
				counter = 0
		return max(max1, counter)


class Solution:

	def maxConsecutiveOnes(self, N):
		x = bin(N)
		x = x[2:]
		c = 0
		mxc = 0
		for i in x:
			if i == '1':
				c += 1
			else:
				c = 0
			if mxc < c:
				mxc = c
		return mxc


class Solution:

	def maxConsecutiveOnes(self, N):
		maxCount = 0
		count = 0
		while N > 0:
			r = N % 2
			N //= 2
			if r == 1:
				count += 1
				maxCount = count if count > maxCount else maxCount
			else:
				count = 0
		return maxCount


class Solution:

	def maxConsecutiveOnes(self, N):
		bi = bin(N)[2:]
		binary = bi.split('0')
		maxi = 0
		for i in binary:
			if '1' in i:
				maxi = max(maxi, len(i))
		return maxi


class Solution:

	def maxConsecutiveOnes(self, N):
		n = N
		bit_count = 0
		max_count = 0
		while n:
			if n & 1 == 1:
				bit_count += 1
			else:
				bit_count = 0
			n >>= 1
			if bit_count > max_count:
				max_count = bit_count
		return max_count


class Solution:

	def maxConsecutiveOnes(self, N):
		bin_N = bin(N)
		bin_N = bin_N[2:]
		max_len = 0
		count = 0
		for (num, i) in enumerate(bin_N):
			if i == '1':
				count += 1
				if num == len(bin_N) - 1 and count > max_len:
					max_len = count
			elif count > max_len:
				max_len = count
				count = 0
			else:
				count = 0
		return max_len


class Solution:

	def maxConsecutiveOnes(self, N):
		Temp = bin(N)[2:]
		ctr = 0
		Ans = 0
		for i in Temp:
			if i == '1':
				ctr += 1
			else:
				Ans = max(Ans, ctr)
				ctr = 0
		Ans = max(ctr, Ans)
		return Ans


class Solution:

	def maxConsecutiveOnes(self, N):
		d = 0
		s = ''
		c = 0
		m = 0
		while N > 0:
			d = N % 2
			if d == 1:
				c += 1
			else:
				m = max(m, c)
				c = 0
			N = N // 2
		m = max(m, c)
		return m


class Solution:

	def maxConsecutiveOnes(self, N):
		b = bin(N)[2:]
		s = str(b).split('0')
		l = []
		for i in s:
			l.append(len(i))
		return max(l)


class Solution:

	def maxConsecutiveOnes(self, N):
		m = format(N, 'b')
		c = 0
		l = []
		for i in m:
			if i == '1':
				c += 1
			else:
				l.extend([c])
				c = 0
		l.append(c)
		return max(l)


class Solution:

	def maxConsecutiveOnes(self, N):
		ma = 0
		c = 0
		while N:
			d = N & 1
			if d == 1:
				c += 1
			else:
				c = 0
			if c > ma:
				ma = c
			N = N >> 1
		return ma


class Solution:

	def maxConsecutiveOnes(self, n):
		maxi = 0
		count = 0
		while n != 0:
			if n & 1 == 1:
				count += 1
				maxi = max(maxi, count)
			else:
				count = 0
			n = n >> 1
		return maxi


class Solution:

	def maxConsecutiveOnes(self, N):
		b = bin(N)[2:]
		c = 0
		m = 0
		for i in b:
			if i == '1':
				c += 1
			else:
				if c > m:
					m = c
				c = 0
		if c > m:
			m = c
		return m


class Solution:

	def maxConsecutiveOnes(self, N):
		b = bin(N)[2:]
		n = len(b)
		for x in range(n, 0, -1):
			if '1' * x in b:
				return x
		return 0


class Solution:

	def maxConsecutiveOnes(self, N):
		max_len = 0
		cur_len = 0
		while N:
			if N & 1 == 1:
				cur_len += 1
			else:
				max_len = max(max_len, cur_len)
				cur_len = 0
			N >>= 1
		max_len = max(max_len, cur_len)
		return max_len


class Solution:

	def maxConsecutiveOnes(self, N):
		max_count = 0
		curr_cnt = 0
		curr = 1
		while N > 0:
			res = N & 1
			N = N // 2
			if res:
				curr_cnt += 1
			else:
				max_count = max(max_count, curr_cnt)
				curr_cnt = 0
		max_count = max(max_count, curr_cnt)
		return max_count


class Solution:

	def maxConsecutiveOnes(self, N):
		maxi = 0
		a = str(bin(N))[2:]
		a = a.split('0')
		for i in a:
			if i != None:
				maxi = max(maxi, len(i))
		return maxi


class Solution:

	def maxConsecutiveOnes(self, N):
		ans = 0
		curr = 0
		while N:
			res = N // 2
			rem = N % 2
			if rem == 0:
				curr = 0
			else:
				curr += 1
				ans = max(ans, curr)
			N = res
		return ans


class Solution:

	def maxConsecutiveOnes(self, N):
		a = '{0:b}'.format(N)
		m = 0
		a = a.split('0')
		for lst in a:
			m = max(m, len(lst))
		return m


class Solution:

	def maxConsecutiveOnes(self, N):
		c = 0
		mx = 0
		k = bin(N).replace('0b', '')
		k = str(k)
		for i in k:
			if i == '1':
				c += 1
				mx = max(c, mx)
			else:
				c = 0
		return mx


class Solution:

	def maxConsecutiveOnes(self, N):
		maxcount = 0
		lcount = 0
		while N > 0:
			if N & 1:
				lcount += 1
				maxcount = max(maxcount, lcount)
			else:
				lcount = 0
			N >>= 1
		return maxcount


class Solution:

	def maxConsecutiveOnes(self, n):
		num = []
		while True:
			if n == 1:
				num.append(1)
				break
			if n % 2 == 0:
				num.append(0)
			else:
				num.append(1)
			n = n // 2
		num.reverse()
		count = 0
		maxc = 0
		for i in num:
			if i == 1:
				count += 1
				maxc = max(maxc, count)
			else:
				count = 0
		return maxc


class Solution:

	def maxConsecutiveOnes(self, N):
		count = 0
		x = bin(N)[2:]
		lst = x.split('0')
		for i in lst:
			count = max(count, len(i))
		return count


class Solution:

	def maxConsecutiveOnes(self, N):
		b = str(bin(N)[2:])
		maximum = 0
		count = 0
		for i in range(len(b)):
			if b[i] == '1':
				count += 1
			else:
				maximum = max(count, maximum)
				count = 0
		maximum = max(count, maximum)
		return maximum


class Solution:

	def maxConsecutiveOnes(self, N):
		c = 0
		arr = []
		while N > 0:
			b = N % 2
			if b == 1:
				c = c + 1
			else:
				arr.append(c)
				c = 0
			N = int(N / 2)
		arr.append(c)
		return max(arr)


import math

class Solution:

	def maxConsecutiveOnes(self, N):
		(longest, temp) = (1, N)
		while temp != 0 and temp > 2 ** longest:
			pos = int(math.log2(temp - (temp & temp - 1))) + 1
			temp = temp >> pos
			count = 1
			while temp % 2:
				count += 1
				temp = temp >> 1
			if count > longest:
				longest = count
		return longest