Python Program to check whether it is possible to make a divisible by 3 number using all digits in an array
Last Updated :
14 Mar, 2023
Given an array of integers, 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 “Yes” and if not print “No”.
Examples:
Input : arr[] = {40, 50, 90}
Output : Yes
We can construct a number which is
divisible by 3, for example 945000.
So the answer is Yes.
Input : arr[] = {1, 4}
Output : No
The only possible numbers are 14 and 41,
but both of them are not divisible by 3,
so the answer is No.
def isPossibleToMakeDivisible(arr, n):
remainder = 0
for i in range ( 0 , n):
remainder = (remainder + arr[i]) % 3
return (remainder = = 0 )
arr = [ 40 , 50 , 90 ];
n = 3
if (isPossibleToMakeDivisible(arr, n)):
print ( "Yes" )
else :
print ( "No" )
|
Time Complexity : O(n)
Please refer complete article on Possible to make a divisible by 3 number using all digits in an array for more details!
Share your thoughts in the comments
Please Login to comment...