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.
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 if the sum of its digits is divisible by 3. So we simply find the sum of array elements. If the sum is divisible by 3, our answer is Yes, else No
Implementation:
CPP
#include <bits/stdc++.h>
using namespace std;
bool isPossibleToMakeDivisible( int arr[], int n)
{
int remainder = 0;
for ( int i=0; i<n; i++)
remainder = (remainder + arr[i]) % 3;
return (remainder == 0);
}
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;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
public static boolean isPossibleToMakeDivisible( int arr[], int n)
{
int remainder = 0 ;
for ( int i= 0 ; i<n; i++)
remainder = (remainder + arr[i]) % 3 ;
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" );
}
}
|
Python3
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" )
|
C#
using System;
class GFG
{
public static bool isPossibleToMakeDivisible( int []arr, int n)
{
int remainder = 0;
for ( int i = 0; i < n; i++)
remainder = (remainder + arr[i]) % 3;
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" );
}
}
|
PHP
<?php
function isPossibleToMakeDivisible( $arr , $n )
{
$remainder = 0;
for ( $i = 0; $i < $n ; $i ++)
$remainder = ( $remainder + $arr [ $i ]) % 3;
return ( $remainder == 0);
}
$arr = array ( 40, 50, 90 );
$n = sizeof( $arr );
if (isPossibleToMakeDivisible( $arr , $n ))
echo ( "Yes\n" );
else
echo ( "No\n" );
?>
|
Javascript
<script>
function isPossibleToMakeDivisible(arr , n)
{
var remainder = 0;
for (i=0; i<n; i++)
remainder = (remainder + arr[i]) % 3;
return (remainder == 0);
}
var arr = [ 40, 50, 90 ];
var n = 3;
if (isPossibleToMakeDivisible(arr, n))
document.write( "Yes\n" );
else
document.write( "No\n" );
</script>
|
Time Complexity: O(n)
Space Complexity: O(1)
Last Updated :
29 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...