Possible to make a divisible by 3 number using all digits in an array
Last Updated :
29 Jul, 2022
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
// 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;
}
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)>
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
<?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.
?>
JavaScript
<script>
// javascript 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
var remainder = 0;
for (i=0; i<n; i++)
remainder = (remainder + arr[i]) % 3;
// Return true if remainder is 0.
return (remainder == 0);
}
var arr = [ 40, 50, 90 ];
var n = 3;
if (isPossibleToMakeDivisible(arr, n))
document.write("Yes\n");
else
document.write("No\n");
// This code contributed by Princi Singh
</script>
Time Complexity: O(n)
Space Complexity: O(1)
Similar Reads
POTD Solutions | 20 Octâ 23 | Form a number divisible by 3 using array digits View all POTD Solutions Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of basic mathematics but will also help you build up probl
3 min read
Smallest number with sum of digits as N and divisible by 10^N Find the smallest number such that the sum of its digits is N and it is divisible by 10^N . Examples : Input : N = 5 Output : 500000 500000 is the smallest number divisible by 10^5 and sum of digits as 5. Input : N = 20 Output : 29900000000000000000000Recommended PracticeSmallest number with sum of
6 min read
Count all Arrays within a Range and Divisible by 3 Given three integers n,l,r, the task is to determine the count of such arrays that follow the below two conditions: All the elements of the array are between l to r (inclusive) The sum of all the elements is divisible by 3.Since the answer can be very long, print the remainder when the result is div
9 min read
Number of digits to be removed to make a number divisible by 3 Given a very large number num (1 <= num <= 10^1000), print the number of digits that needs to be removed to make the number exactly divisible by 3. If it is not possible then print -1.Examples : Input: num = "1234"Output: 1Explanation: we need to remove one digit that is 1 or 4, to make thenum
13 min read
Print digit's position to be removed to make a number divisible by 6 Given a number N, remove exactly one digit to make the number divisible by 6 (make it the largest possible). Print the position that has to be removed, If not possible then print -1. Examples: Input: 123 Output: 3 Explanation: Remove 3rd position element and hence the number is 12, which is divisibl
15 min read
Sum of n digit numbers divisible by a given number Given n and a number, the task is to find the sum of n digit numbers that are divisible by given number.Examples: Input : n = 2, number = 7Output : 728Explanation: There are thirteen n digit numbers that are divisible by 7. Numbers are : 14+ 21 + 28 + 35 + 42 + 49 + 56 + 63 +70 + 77 + 84 + 91 + 98.
9 min read