Given a number S. Check whether it is divisble by 11 or not.
Example 1:
Input:
S = 76945
Output: 1
Explanation: The number is divisible by 11
as 76945 % 11 = 0.
Ã¢â¬â¹Example 2:
Input: 
S = 12
Output: 0
Explanation: The number is not divisible
by 11 as 12 % 11 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function divisibleBy11() which takes the number in the form of string S as input and returns 1 if the number is divisible by 11. Else, it returns 0.
Expected Time Complexity: O(Log S) where S is the input number.
Expected Auxiliary Space: O(1). 
Constraints:
1<=S<=10^{1000}+5

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Mathematics

Starter code: #User function Template for python3
class Solution:
	def divisibleBy11(self, S):
		# Your Code Here

Solutions:
class Solution:

	def divisibleBy11(self, S):
		if int(S) % 11 == 0:
			return 1
		else:
			return 0


class Solution:

	def divisibleBy11(self, S):
		odd = 0
		even = 0
		for i in range(len(str(S))):
			if i % 2 == 0:
				even += int(str(S)[i])
			else:
				odd += int(str(S)[i])
		if abs(even - odd) % 11 == 0:
			return 1
		return 0


class Solution:

	def divisibleBy11(self, S):
		return int(not int(S) % 11)


class Solution:

	def divisibleBy11(self, s):
		s = int(s)
		if s % 11 == 0:
			return 1
		return 0


class Solution:

	def divisibleBy11(self, S):
		even = S[0::2]
		odd = S[1::2]
		even = [int(x) for x in even]
		odd = [int(x) for x in odd]
		diff = sum(even) - sum(odd)
		return int(diff % 11 == 0)


class Solution:

	def divisibleBy11(self, s):
		(se, so) = (0, 0)
		for i in range(len(s)):
			if i % 2 == 0:
				se += int(s[i])
			else:
				so += int(s[i])
		if abs(se - so) % 11 == 0:
			return 1
		else:
			return 0


class Solution:

	def divisibleBy11(self, S):
		(num, e, o) = (str(S), 0, 0)
		for i in range(len(num)):
			if i % 2 == 0:
				e += int(num[i])
			else:
				o += int(num[i])
		if (e - o) % 11 == 0:
			return 1
		return 0


class Solution:

	def divisibleBy11(self, S):
		return int(S % 11 == 0)