# Possible to make a divisible by 3 number using all digits in an array

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.

Input : arr[] = {1, 4}
Output : No
The only possible numbers are 14 and 41,
but both of them are not divisible by 3,
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

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 ` `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

## 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 `

## 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)> `

## 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 `

## PHP

 ` `

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : jit_t

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.