Skip to content
Related Articles
Open in App
Not now

Related Articles

Java Program to check whether it is possible to make a divisible by 3 number using all digits in an array

Improve Article
Save Article
  • Last Updated : 12 Sep, 2022
Improve Article
Save Article

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.


// Java program to find if it is possible
// to make a number divisible by 3 using
// all digits of given array
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))
// Code Contributed by Mohit Gupta_OMG <(0_o)>



Time Complexity: O(n)

Auxiliary Space: O(1) because it is not using extra space

 Please refer complete article on Possible to make a divisible by 3 number using all digits in an array for more details!

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!