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.

The idea is based on the fact that a number is divisible by 3 iff sum of its digits is divisible by 3. So we simply find sum of array elements. If the sum is divisible by 3, our answer is Yes, else No.

## CPP

`// C++ program to find if it is possible ` `// to make a number divisible by 3 using ` `// all digits of given array ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `bool` `isPossibleToMakeDivisible(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// Find remainder of sum when divided by 3 ` ` ` `int` `remainder = 0; ` ` ` `for` `(` `int` `i=0; i<n; i++) ` ` ` `remainder = (remainder + arr[i]) % 3; ` ` ` ` ` `// Return true if remainder is 0. ` ` ` `return` `(remainder == 0); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 40, 50, 90 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `if` `(isPossibleToMakeDivisible(arr, n)) ` ` ` `printf` `(` `"Yes\n"` `); ` ` ` `else` ` ` `printf` `(` `"No\n"` `); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find if it is possible ` `// to make a number divisible by 3 using ` `// all digits of given array ` ` ` `import` `java.io.*; ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `public` `static` `boolean` `isPossibleToMakeDivisible(` `int` `arr[], ` `int` `n) ` ` ` `{ ` ` ` `// Find remainder of sum when divided by 3 ` ` ` `int` `remainder = ` `0` `; ` ` ` `for` `(` `int` `i=` `0` `; i<n; i++) ` ` ` `remainder = (remainder + arr[i]) % ` `3` `; ` ` ` ` ` `// Return true if remainder is 0. ` ` ` `return` `(remainder == ` `0` `); ` ` ` `} ` ` ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `arr[] = { ` `40` `, ` `50` `, ` `90` `}; ` ` ` `int` `n = ` `3` `; ` ` ` `if` `(isPossibleToMakeDivisible(arr, n)) ` ` ` `System.out.print(` `"Yes\n"` `); ` ` ` `else` ` ` `System.out.print(` `"No\n"` `); ` ` ` `} ` `} ` ` ` `// Code Contributed by Mohit Gupta_OMG <(0_o)> ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to find if it is possible ` `# to make a number divisible by 3 using ` `# all digits of given array ` ` ` `def` `isPossibleToMakeDivisible(arr, n): ` ` ` `# Find remainder of sum when divided by 3 ` ` ` `remainder ` `=` `0` ` ` `for` `i ` `in` `range` `(` `0` `, n): ` ` ` `remainder ` `=` `(remainder ` `+` `arr[i]) ` `%` `3` ` ` ` ` `# Return true if remainder is 0. ` ` ` `return` `(remainder ` `=` `=` `0` `) ` ` ` `# main() ` ` ` `arr ` `=` `[` `40` `, ` `50` `, ` `90` `]; ` `n ` `=` `3` `if` `(isPossibleToMakeDivisible(arr, n)): ` ` ` `print` `(` `"Yes"` `) ` `else` `: ` ` ` `print` `(` `"No"` `) ` ` ` `# Code Contributed by Mohit Gupta_OMG <(0_o)> ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find if it is possible ` `// to make a number divisible by 3 using ` `// all digits of given array ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `public` `static` `bool` `isPossibleToMakeDivisible(` `int` `[]arr, ` `int` `n) ` ` ` `{ ` ` ` `// Find remainder of sum when divided by 3 ` ` ` `int` `remainder = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `remainder = (remainder + arr[i]) % 3; ` ` ` ` ` `// Return true if remainder is 0. ` ` ` `return` `(remainder == 0); ` ` ` `} ` ` ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `[]arr = { 40, 50, 90 }; ` ` ` `int` `n = 3; ` ` ` ` ` `if` `(isPossibleToMakeDivisible(arr, n)) ` ` ` `Console.WriteLine(` `"Yes"` `); ` ` ` `else` ` ` `Console.WriteLine(` `"No"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find if it is possible ` `// to make a number divisible by 3 using ` `// all digits of given array ` ` ` `function` `isPossibleToMakeDivisible(` `$arr` `, ` `$n` `) ` `{ ` ` ` ` ` `// Find remainder of sum ` ` ` `// when divided by 3 ` ` ` `$remainder` `= 0; ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `$remainder` `= (` `$remainder` `+ ` `$arr` `[` `$i` `]) % 3; ` ` ` ` ` `// Return true if remainder is 0. ` ` ` `return` `(` `$remainder` `== 0); ` `} ` ` ` `// Driver code ` `$arr` `= ` `array` `( 40, 50, 90 ); ` `$n` `= sizeof(` `$arr` `); ` `if` `(isPossibleToMakeDivisible(` `$arr` `, ` `$n` `)) ` ` ` `echo` `(` `"Yes\n"` `); ` `else` ` ` `echo` `(` `"No\n"` `); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Yes

Time Complexity : O(n)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum digits to be removed to make either all digits or alternating digits same
- Check if the sum of digits of number is divisible by all of its digits
- Smallest N digit number divisible by all possible prime digits
- Number of digits to be removed to make a number divisible by 3
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Sum of all N digit palindromic numbers divisible by 9 formed using digits 1 to 9
- Minimum operations required to make all Array elements divisible by K
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Generate all possible permutations of a Number divisible by N
- Count different numbers possible using all the digits their frequency times
- Minimum number to be added to all digits of X to make X > Y
- Minimum number of swaps required to make a number divisible by 60
- Check if it is possible to make array increasing or decreasing by rotating the array
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Find the Largest number with given number of digits and sum of digits
- Number of digits in the nth number made of given four digits
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Find smallest number with given number of digits and sum of digits under given constraints

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.