Smallest multiple of 3 which consists of three given non-zero digits

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the minimum number
// divisible by 3 formed by the given digits
int printSmallest(int a[3])
{
    int sum, sum1;
  
    // Sort the given array in ascending
    sort(a, a + 3);
  
    int i, j, k, num;
  
    // Check if any single digit is divisible by 3
    for (int i = 0; i < 3; i++) {
        if (a[i] % 3 == 0)
            return a[i];
    }
  
    // Check if any two digit number formed by
    // the given digits is divisible by 3
    // starting from the minimum
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
  
            // Generate the two digit number
            num = (a[i] * 10) + a[j];
            if (num % 3 == 0)
                return num;
        }
    }
  
    // If none of the above is true, we can
    // form three digit number by taking a[0]
    // three times.
    return a[0]*100 + a[0]*10 + a[0];
}
  
// Driver code
int main()
{
    int arr[] = { 7, 7, 1 };
    cout << printSmallest(arr);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.util.Arrays;
public class GFG {
  
// Function to return the minimum number
// divisible by 3 formed by the given digits
    static int printSmallest(int a[]) {
        int sum, sum1;
  
        // Sort the given array in ascending
        Arrays.sort(a);
  
        int i, j, k, num;
  
        // Check if any single digit is divisible by 3
        for (i = 0; i < 3; i++) {
            if (a[i] % 3 == 0) {
                return a[i];
            }
        }
  
        // Check if any two digit number formed by
        // the given digits is divisible by 3
        // starting from the minimum
        for (i = 0; i < 3; i++) {
            for (j = 0; j < 3; j++) {
  
                // Generate the two digit number
                num = (a[i] * 10) + a[j];
                if (num % 3 == 0) {
                    return num;
                }
            }
        }
  
        // If none of the above is true, we can
        // form three digit number by taking a[0]
        // three times.
        return a[0] * 100 + a[0] * 10 + a[0];
    }
  
// Driver code
    public static void main(String[] args) {
  
        int arr[] = {7, 7, 1};
        System.out.println(printSmallest(arr));
  
    }
}
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to return the minimum 
# number divisible by 3 formed by 
# the given digits
def printSmallest(a, n):
  
    sum0, sum1 = 0, 0
  
    # Sort the given array in ascending
    a = sorted(a)
  
    # Check if any single digit is
    # divisible by 3
    for i in range(n):
        if (a[i] % 3 == 0):
            return a[i]
  
    # Check if any two digit number 
    # formed by the given digits is 
    # divisible by 3 starting from 
    # the minimum
    for i in range(n):
        for j in range(n):
              
            # Generate the two digit number
            num = (a[i] * 10) + a[j]
            if (num % 3 == 0):
                return num
  
    # If none of the above is true, we can
    # form three digit number by taking a[0]
    # three times.
    return a[0] * 100 + a[0] * 10 + a[0]
  
# Driver code
arr = [7, 7, 1 ]
n = len(arr)
  
print(printSmallest(arr, n))
  
# This code is conteibuted
# by mohit kumar 29

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

      
// C# implementation of the approach
using System;
  
public class GFG {
   
// Function to return the minimum number
// divisible by 3 formed by the given digits
    static int printSmallest(int []a) {
   
        // Sort the given array in ascending
        Array.Sort(a);
   
        int i, j, num;
   
        // Check if any single digit is divisible by 3
        for (i = 0; i < 3; i++) {
            if (a[i] % 3 == 0) {
                return a[i];
            }
        }
   
        // Check if any two digit number formed by
        // the given digits is divisible by 3
        // starting from the minimum
        for (i = 0; i < 3; i++) {
            for (j = 0; j < 3; j++) {
   
                // Generate the two digit number
                num = (a[i] * 10) + a[j];
                if (num % 3 == 0) {
                    return num;
                }
            }
        }
   
        // If none of the above is true, we can
        // form three digit number by taking a[0]
        // three times.
        return a[0] * 100 + a[0] * 10 + a[0];
    }
   
// Driver code
    public static void Main() {
   
        int []arr = {7, 7, 1};
        Console.Write(printSmallest(arr));
   
    }
}
//This code is contributed by Rajput-Ji

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach 
  
// Function to return the minimum number 
// divisible by 3 formed by the given digits 
function printSmallest($a)
{
  
    // Sort the given array in ascending 
    sort($a); 
  
    // Check if any single digit 
    // is divisible by 3 
    for ($i = 0; $i < 3; $i++) 
    
        if ($a[$i] % 3 == 0) 
            return $a[$i];
    
  
    // Check if any two digit number formed 
    // by the given digits is divisible by 3 
    // starting from the minimum 
    for ($i = 0; $i < 3; $i++) 
    
        for ($j = 0; $j < 3; $j++) 
        
  
            // Generate the two digit number 
            $num = ($a[$i] * 10) + $a[$j]; 
            if ($num % 3 == 0) 
                return $num
        
    
  
    // If none of the above is true, we can 
    // form three digit number by taking a[0] 
    // three times. 
    return $a[0] * 100 + $a[0] * 10 + $a[0]; 
  
// Driver code 
$arr = array( 7, 7, 1 );
echo printSmallest($arr);
  
// This code is contributed by Ryuga
?>

chevron_right


Output:

111


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.