Given a number in its binary form find if the given binary number is a multiple of 3. It is recommended to finish the task using one traversal of input binary number.
Example 1:
Input: S = "0011"
Output: 1
Explanation: "0011" is 3, which is divisible by 3.
Example 2:
Input: S = "100"
Output: 0
Explanation: "100"'s decimal equivalent is 4, which is not divisible by 3.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function isDivisible() which takes the string s as inputs and returns 1 if the given number is divisible by 3 otherwise 0.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ |S| ≤ 10^{5}

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: Bit manipulation

Data structures

Mathematics

Starter code: #User function Template for python3

class Solution:

	def isDivisible(self, s):

		# code here

Solutions:
class Solution:

	def isDivisible(self, s):
		n = int(s, 2)
		if n % 3 == 0:
			return 1
		return 0


class Solution:

	def isDivisible(self, s):
		k = 0
		res = int(s, 2)
		if res % 3 == 0:
			return 1
		return 0


class Solution:

	def isDivisible(self, s):
		x = int(s, 2)
		if x % 3 == 0:
			return 1
		else:
			return 0


class Solution:

	def isDivisible(self, s):
		even = 0
		odd = 0
		for i in range(len(s)):
			if s[i] == '1':
				if i % 2 == 0:
					even += 1
				else:
					odd += 1
		if abs(odd - even) % 3 == 0:
			return 1
		else:
			return 0


class Solution:

	def isDivisible(self, s):
		res = 0
		for i in range(len(s)):
			if s[i] == '1':
				if i % 2 == 0:
					res += 1
				else:
					res += 2
		if res % 3 == 0:
			return 1
		return 0


import math

class Solution:

	def isDivisible(self, s):
		d = int(s, 2)
		if d % 3 == 0:
			return 1
		else:
			return 0


class Solution:

	def isDivisible(self, s):
		(od, ev) = (0, 0)
		for i in range(len(s)):
			if s[i] == '1':
				if i & 1:
					od += 1
				else:
					ev += 1
		ans = abs(od - ev)
		if ans % 3 == 0:
			return 1
		return 0


class Solution:

	def isDivisible(self, s):
		e = 0
		o = 0
		for i in range(len(s)):
			if i % 2 == 0:
				e += int(s[i])
			else:
				o += int(s[i])
		diff = abs(e - o)
		if diff % 3 == 0:
			return 1
		else:
			return 0


class Solution:

	def isDivisible(self, s):
		rem = 0
		for i in s:
			rem = (rem * 2 + int(i)) % 3
		if rem == 0:
			return 1
		else:
			return 0


import math

class Solution:

	def isDivisible(self, s):
		g = int(s, 2)
		h = g % 3
		if h == 0:
			return 1
		else:
			return 0


class Solution:

	def isDivisible(self, s):
		return int(int(s, 2) % 3 == 0)


class Solution:

	def isDivisible(self, s):
		c = str(int(s, 2))
		d = 0
		for i in c:
			d = d + int(i)
		if d % 3 == 0:
			return 1
		else:
			return 0


class Solution:

	def isDivisible(self, s):
		n = len(s)
		even_sum = odd_sum = 0
		for i in range(n - 1, -1, -1):
			if s[i] == '1':
				if (n - i - 1) % 2 == 0:
					even_sum += 1
				else:
					odd_sum += 1
		return (abs(even_sum - odd_sum) % 3 == 0) * 1


class Solution:

	def isDivisible(self, s):
		i = 0
		n = len(s)
		s = s[::-1]
		ans = 0
		j = 1
		num = 1
		while i < n:
			if s[i] == '1':
				ans += num
			i += 1
			if num == 1:
				num = 2
			else:
				num *= 2
		return 1 if not ans % 3 else 0


class Solution:

	def isDivisible(self, s):
		multi = 0
		for i in s:
			multi = multi * 2 + int(i)
		if multi % 3 == 0:
			return 1
		return 0


class Solution:

	def isDivisible(self, s):
		st = 0
		for i in range(len(s)):
			if st == 0:
				if s[i] == '1':
					st = 1
			elif st == 1:
				if s[i] == '1':
					st = 0
				else:
					st = 2
			elif s[i] == '0':
				st = 1
		return int(st == 0)


class Solution:

	def isDivisible(self, s):
		n = len(s)
		j = 0
		ans = 0
		for i in range(n - 1, -1, -1):
			if s[i] == '1':
				ans += 2 << j
			else:
				ans += 0
			j += 1
		if ans % 3 == 0:
			return 1
		else:
			return 0


class Solution:

	def isDivisible(self, s):
		x = len(s) - 1
		summ = 0
		j = 0
		for i in range(x, -1, -1):
			summ = summ + (1 << i) * int(s[j])
			j = j + 1
		if summ % 3 == 0:
			return 1
		return 0


class Solution:

	def isDivisible(self, s):
		a = len(s) - 1
		sum = 0
		k = 0
		for n in range(a, -1, -1):
			sum += (1 << n) * int(s[k])
			k += 1
		if sum % 3 == 0:
			return 1
		else:
			return 0


class Solution:

	def isDivisible(self, s):
		s = '0b' + s
		n = eval(s)
		if not n % 3:
			return 1
		return 0


class Solution:

	def isDivisible(self, s):
		number = 0
		k = len(s) - 1
		for i in range(len(s)):
			number = number + int(s[i]) * (1 << k)
			k = k - 1
		if number % 3 == 0:
			return 1
		else:
			return 0


class Solution:

	def isDivisible(self, S):
		(oddPosition, evenPosition) = (0, 0)
		for i in range(len(S)):
			if i % 2 and S[i] == '1':
				oddPosition += 1
			if i % 2 == 0 and S[i] == '1':
				evenPosition += 1
		return 1 if abs(oddPosition - evenPosition) % 3 == 0 else 0


class Solution:

	def isDivisible(self, s):
		ev = 0
		od = 0
		for i in range(len(s)):
			if i & 1 and s[i] == '1':
				od += 1
			elif i & 1 == 0 and s[i] == '1':
				ev += 1
		if abs(od - ev) % 3 == 0:
			return 1
		return 0


class Solution:

	def isDivisible(self, s):
		(oddsetbits, evensetbits) = (0, 0)
		n = len(s)
		for (i, c) in enumerate(s):
			if c == '1':
				if i & 1:
					oddsetbits += 1
				else:
					evensetbits += 1
		return 1 if not abs(oddsetbits - evensetbits) % 3 else 0


class Solution:

	def isDivisible(self, s):
		num = 0
		for i in s:
			num = num * 2 + int(i)
		return int(num % 3 == 0)

	def isDivisible(self, s):
		return int(int(s, 2) % 3 == 0)

	def isDivisible(self, s):
		(pos, even, odd) = (0, 0, 0)
		for i in s:
			if pos % 2:
				if s[pos] == '1':
					odd += 1
			elif s[pos] == '1':
				even += 1
			pos += 1
		return int((odd - even) % 3 == 0)


class Solution:

	def isDivisible(self, s):
		num = 0
		for i in s:
			num = num * 2 + int(i)
		return int(num % 3 == 0)

	def isDivisible(self, s):
		return int(int(s, 2) % 3 == 0)


class Solution:

	def isDivisible(self, s):
		e = 0
		o = 0
		for i in range(len(s)):
			if i & 1 == 0 and s[i] == '1':
				e += 1
			if i & 1 and s[i] == '1':
				o += 1
		if (e - o) % 3 == 0:
			return 1
		return 0