Smallest multiple of 3 which consists of three given non-zero digits
Last Updated :
23 Jun, 2022
Given three non-zero digits 0 < A, B, C < 9. The task is to find the smallest number divisible by 3 all of whose digits are in set {A, B, C}.
Note: It is not necessary to include all three digits. The result can be A, AA, AB, CCA etc.
Examples:
Input: A = 2, B = 4, C = 7
Output: 24
24 is the minimum number divisible by 3 that can be formed by the given digits.
Input: A = 1, B = 2, C = 3
Output: 3
Approach: Take all three numbers in an array and sort them in increasing order. Now check if any number is divisible by 3, if yes then the number will be the answer.
If not then again check if by taking any of two numbers. Finally take smallest number and our result is this number repeated three times.
Why can’t we get answer of length more than three digits?
Even if any number is not divisible by 3, repeating the smallest number 3 times will make it divisible by 3. Note that a number is divisible by 3 if sum of its digits is divisible by 3.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int printSmallest( int a[3])
{
int sum, sum1;
sort(a, a + 3);
int i, j, k, num;
for ( int i = 0; i < 3; i++) {
if (a[i] % 3 == 0)
return a[i];
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
num = (a[i] * 10) + a[j];
if (num % 3 == 0)
return num;
}
}
return a[0]*100 + a[0]*10 + a[0];
}
int main()
{
int arr[] = { 7, 7, 1 };
cout << printSmallest(arr);
return 0;
}
|
Java
import java.util.Arrays;
public class GFG {
static int printSmallest( int a[]) {
int sum, sum1;
Arrays.sort(a);
int i, j, k, num;
for (i = 0 ; i < 3 ; i++) {
if (a[i] % 3 == 0 ) {
return a[i];
}
}
for (i = 0 ; i < 3 ; i++) {
for (j = 0 ; j < 3 ; j++) {
num = (a[i] * 10 ) + a[j];
if (num % 3 == 0 ) {
return num;
}
}
}
return a[ 0 ] * 100 + a[ 0 ] * 10 + a[ 0 ];
}
public static void main(String[] args) {
int arr[] = { 7 , 7 , 1 };
System.out.println(printSmallest(arr));
}
}
|
Python3
def printSmallest(a, n):
sum0, sum1 = 0 , 0
a = sorted (a)
for i in range (n):
if (a[i] % 3 = = 0 ):
return a[i]
for i in range (n):
for j in range (n):
num = (a[i] * 10 ) + a[j]
if (num % 3 = = 0 ):
return num
return a[ 0 ] * 100 + a[ 0 ] * 10 + a[ 0 ]
arr = [ 7 , 7 , 1 ]
n = len (arr)
print (printSmallest(arr, n))
|
C#
using System;
public class GFG {
static int printSmallest( int []a) {
Array.Sort(a);
int i, j, num;
for (i = 0; i < 3; i++) {
if (a[i] % 3 == 0) {
return a[i];
}
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
num = (a[i] * 10) + a[j];
if (num % 3 == 0) {
return num;
}
}
}
return a[0] * 100 + a[0] * 10 + a[0];
}
public static void Main() {
int []arr = {7, 7, 1};
Console.Write(printSmallest(arr));
}
}
|
PHP
<?php
function printSmallest( $a )
{
sort( $a );
for ( $i = 0; $i < 3; $i ++)
{
if ( $a [ $i ] % 3 == 0)
return $a [ $i ];
}
for ( $i = 0; $i < 3; $i ++)
{
for ( $j = 0; $j < 3; $j ++)
{
$num = ( $a [ $i ] * 10) + $a [ $j ];
if ( $num % 3 == 0)
return $num ;
}
}
return $a [0] * 100 + $a [0] * 10 + $a [0];
}
$arr = array ( 7, 7, 1 );
echo printSmallest( $arr );
?>
|
Javascript
<script>
function printSmallest(a) {
a.sort( function (a, b){ return a - b});
let i, j, num;
for (i = 0; i < 3; i++) {
if (a[i] % 3 == 0) {
return a[i];
}
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
num = (a[i] * 10) + a[j];
if (num % 3 == 0) {
return num;
}
}
}
return a[0] * 100 + a[0] * 10 + a[0];
}
let arr = [7, 7, 1];
document.write(printSmallest(arr));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...