We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
A number is only divisible by 3 if the sum of its digits is divisible by 3.
So make a loop to sum the digits into a variable. You'll only need to check if that one number is divisible by 3. If yes, then any combination of its digits will be divisible by 3.
def canConstruct(a):
digits_sum = 0
for i in a:
str_i = str(i)
for x in str_i:
digits_sum += int(x)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Constructing a Number
You are viewing a single comment's thread. Return to all comments →
A number is only divisible by 3 if the sum of its digits is divisible by 3.
So make a loop to sum the digits into a variable. You'll only need to check if that one number is divisible by 3. If yes, then any combination of its digits will be divisible by 3.