Given an array arr of integers of length N, the task is to find whether it’s possible to construct an integer using all the digits of these numbers such that it would be divisible by 3. If it is possible then print “1” and if not print “0”.
Example 1:
Input: N = 3
arr = {40, 50, 90}
Output: 1
Explaination: One such number is 405900.
Example 2:
Input: N = 2
arr = {1, 4}
Output: 0
Explaination: The numbers we can form 
are 14 and 41. But neither of them are 
divisible by 3.
Your Task:
You do not need to read input or print anything. Your task is to complete the function isPossible() which takes N and arr as input parameters and returns 1 if we can form a number by the digits of the given number, otherwise returns 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N, arr[i] ≤ 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Mathematics

Starter code: #User function Template for python3

class Solution:
    def isPossible(self, N, arr):
        # code here

Solutions:
class Solution:

	def isPossible(self, N, arr):
		total = 0
		for i in arr:
			total += i
		if total % 3 == 0:
			return 1
		return 0


class Solution:

	def isPossible(self, N, arr):
		su = 0
		for i in range(N):
			while arr[i] > 0:
				su += arr[i] % 10
				arr[i] = arr[i] // 10
		return 1 if su % 3 == 0 else 0


class Solution:

	def isPossible(self, N, arr):
		sum = 0
		for i in arr:
			sum = i + sum
		if sum % 3 == 0:
			return 1
		else:
			return 0


class Solution:

	def isPossible(self, N, arr):
		sm = 0
		for i in range(N):
			sm += arr[i]
		s = 0
		while sm != 0:
			s = s + int(sm % 10)
			sm = int(sm / 10)
		if s % 3 == 0:
			return '1'
		else:
			return '0'


class Solution:

	def isPossible(self, N, arr):
		if sum(arr) % 3 == 0:
			return 1
		return 0


class Solution:

	def isPossible(self, N, arr):
		sum = 0
		for i in arr:
			while i != 0:
				sum += i % 10
				i //= 10
		if sum % 3:
			return 0
		return 1


class Solution:

	def isPossible(self, N, arr):
		sumi = 0
		for i in range(N):
			sumi += sum(list(map(int, list(str(arr[i])))))
		if sumi % 3 == 0:
			return 1
		else:
			return 0


class Solution:

	def isPossible(self, N, arr):
		reminder = 0
		for elem in arr:
			reminder += elem % 3
		return 1 if reminder % 3 == 0 else 0


class Solution:

	def isPossible(self, N, arr):
		if sum(arr) % 3 == 0:
			return 1
		sum_all = sum(arr)
		sum_str = str(sum_all)
		sum_int = 0
		for i in sum_str:
			sum_int += int(i)
		if sum_int % 3 == 0:
			return 1
		else:
			return 0


class Solution:

	def isPossible(self, N, arr):
		output = 0
		for item in arr:
			output += item % 3
		return int(output % 3 == 0)


class Solution:

	def isPossible(self, N, arr):
		total = 0
		for item in arr:
			total += item
		if total % 3 == 0:
			return 1
		else:
			return 0


class Solution:

	def isPossible(self, N, arr):
		s = ''
		for i in arr:
			s += str(i)
		s += str(0)
		n = 0
		for i in s:
			n += int(i)
		if n % 3 == 0:
			return 1
		return 0


class Solution:

	def isPossible(self, N, arr):
		a = 0
		for i in arr:
			b = str(i)
			for j in b:
				a = a + int(j)
		if a % 3 == 0:
			return 1
		else:
			return 0


class Solution:

	def isPossible(self, N, arr):
		sum = 0
		for i in range(N):
			for digit in str(arr[i]):
				sum += int(digit)
		if sum % 3 == 0:
			return 1
		else:
			return 0


class Solution:

	def isPossible(self, N, arr):
		s = 0
		for i in range(N):
			s += arr[i]
		if s % 3 == 0:
			return 1
		else:
			return 0


from itertools import permutations as p

class Solution:

	def isPossible(self, N, arr):
		s = sum(arr) % 9
		if s % 3 == 0:
			return 1
		return 0


class Solution:

	def isPossible(self, N, arr):
		z = ''
		for i in arr:
			z += str(i)
		sum = 0
		for i in z:
			sum += int(i)
		if sum % 3 == 0:
			return 1
		return 0


class Solution:

	def isPossible(self, N, arr):
		st = ''
		for i in arr:
			st += str(i)
		summ = 0
		for i in st:
			summ += int(i)
		if summ % 3 == 0:
			return 1
		return 0


class Solution:

	def isPossible(self, N, arr):
		for i in range(len(arr)):
			arr[i] = str(arr[i])
		l = ''.join(arr)
		x = 0
		for i in l:
			x += int(i)
		if x % 3 == 0:
			return 1
		else:
			return 0


class Solution:

	def isPossible(self, N, arr):
		x = ''
		for i in arr:
			x += str(i)
		if int(x) % 3 == 0:
			return 1
		else:
			return 0


class Solution:

	def digitSum(self, N):
		return sum(map(int, str(N)))

	def isPossible(self, N, arr):
		return int(sum(map(self.digitSum, arr)) % 3 == 0)


class Solution:

	def isPossible(self, N, arr):
		sum = 0
		for ele in arr:
			rem = ele % 3
			sum += rem
		if sum % 3 == 0:
			return 1
		else:
			return 0


class Solution:

	def isPossible(self, N, arr):
		a = ''.join(map(str, arr))
		a = int(a)
		if a % 3 == 0:
			return '1'
		else:
			return '0'


class Solution:

	def isPossible(self, N, arr):
		s = ''
		for i in arr:
			s += str(i)
		(s, t) = (int(s), 0)
		while s != 0:
			t += s % 10
			s = s // 10
		if t % 3 == 0:
			return 1
		return 0


class Solution:

	def isPossible(self, N, arr):
		l = []
		for i in arr:
			l.append(int(str(i).replace('0', '')))
		return 1 if sum(l) % 3 == 0 else 0


class Solution:

	def isPossible(self, N, arr):
		l = ''
		for x in arr:
			l += str(x)
		p = 0
		for x in l:
			p += int(x)
		d = {True: 1, False: 0}
		return d[not p % 3]


class Solution:

	def isPossible(self, N, arr):
		s = 0
		for i in arr:
			subcount = 0
			while i > 0:
				p = i % 10
				subcount = subcount + p
				i = i // 10
			s = s + subcount
		if s % 3 == 0:
			return 1
		else:
			return 0


class Solution:

	def isPossible(self, N, arr):
		ar = list(arr)
		s = sum(ar)
		if s % 3 == 0:
			return 1
		else:
			return 0


class Solution:

	def isPossible(self, N, arr):
		digits = []
		for num in arr:
			digits += [int(d) for d in str(num)]
		return 1 if sum(digits) % 3 == 0 else 0


class Solution:

	def sumDigits(self, number):
		sum = 0
		while number != 0:
			digit = number % 10
			number = number // 10
			sum += digit
		return sum

	def isPossible(self, N, arr):
		sum_digits = 0
		for i in arr:
			sum_digits += self.sumDigits(i)
		if sum_digits % 3 == 0:
			return 1
		return 0


class Solution:

	def isPossible(self, n, arr):
		sum = 0
		for i in range(n):
			num = arr[i]
			while num > 0:
				sum += num % 10
				num = num // 10
		return 1 if sum % 3 == 0 else 0


class Solution:

	def isPossible(self, n, arr):
		s = ''
		for elm in arr:
			s += str(elm)
		s = sorted(s)
		s = ''.join(s)
		if int(s) % 3 == 0:
			return 1
		return 0


class Solution:

	def isPossible(self, N, arr):
		s = 0
		for x in arr:
			s = s + x
		if s % 3 == 0:
			return 1
		return 0


class Solution:

	def isPossible(self, N, arr):
		total_remainder = 0
		for num in arr:
			total_remainder += num % 3
		if total_remainder % 3 == 0:
			return 1
		else:
			return 0


class Solution:

	def isPossible(self, n, a):
		count = 0
		for i in range(n):
			count += a[i] % 3
		if count % 3 == 0:
			return '1'
		else:
			return '0'


class Solution:

	def isPossible(self, N, arr):
		a2 = int(''.join(map(str, arr)))
		if a2 % 3 == 0:
			return 1
		return 0


def isPossibleToMakeDivisible(arr, n):
	remainder = 0
	for i in range(0, n):
		remainder = (remainder + arr[i]) % 3
	return remainder == 0

class Solution:

	def isPossible(self, N, arr):
		return int(isPossibleToMakeDivisible(arr, N))


class Solution:

	def isPossible(self, N, arr):
		i = 0
		sum = 0
		while i < N:
			k = str(arr[i])
			for j in k:
				sum += int(j)
			i += 1
		if sum % 3 == 0:
			return 1
		return 0


class Solution:

	def isPossible(self, N, arr):
		str1 = ''
		sum = 0
		for i in arr:
			k = str(i)
			str1 = str1 + k
		p = str1
		for j in str1:
			sum = sum + int(j)
		xr = sum
		if xr % 3 == 0:
			return 1
		else:
			return 0


class Solution:

	def isPossible(self, N, arr):
		Sum = 0
		for x in arr:
			n = x
			while n > 0:
				Sum += n % 10
				n = n // 10
		if Sum % 3 == 0:
			return 1
		else:
			return 0