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.
defcanConstruct(a):# A number is only divisible by 3 if the sum of its digits is divisible by 3digits_sum=0foriina:str_i=str(i)forxinstr_i:digits_sum+=int(x)return"Yes"ifdigits_sum%3==0else"No"
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.