Skip to content
Related Articles

Related Articles

Improve Article
Largest number divisible by 90 that can be made using 0 and 5
  • Last Updated : 10 May, 2021

Given an array containing N elements. Each element is either 0 or 5. Find the largest number divisible by 90 that can be made using any number of elements of this array and arranging them in any way.

Examples:  

Input : arr[] = {5, 5, 5, 5, 5, 5, 5, 5, 0, 5, 5}
Output : 5555555550

Input : arr[] = {5, 0}
Output : 0 

Since we can choose and permute any number of elements, only the number of 0s and 5s in the array matter. So let’s store the count as c0 and c5 respectively. 
The number has to be made a multiple of 90 which is 9*10. Therefore, the number has to be a multiple of both 9 and 10. 
The divisibility rules are as follows:  

  • For a number to be divisible by 10, it should end with 0.
  • For a number to be divisible by 9, the sum of digits should be divisible by 9. Since the only non-zero digit allowed to use is 5, the number of times we use 5 has to be a multiple of 9, so that the sum will be a multiple of 45, i.e divisible by 9.

There are 3 possibilities:  

  • c0=0 . This implies that no number can be made divisible by 10.
  • c5=0. This implies that the only number that can be made divisible by 90 is 0.
  • If both the above conditions are false. Let’s group the number of 5s into groups of 9. There are going to be floor(c5/9) groups that are completely filled, we can use all of the 5s of all the groups to get the number of 5s a multiple of 9 which also makes the digit sum a multiple of 9. Since increasing the number of zeroes does not affect the divisibility, we can use all the zeroes.

Below is the implementation of the above approach: 
 



C++




// CPP program to find largest number
// divisible by 90 that can be made
// using 0 and 5
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find largest number
// divisible by 90 that can be made
// using 0 and 5
void printLargestDivisible(int n, int a[])
{
    // Count of 0s and 5s
    int i, c0 = 0, c5 = 0;
    for (i = 0; i < n; i++) {
        if (a[i] == 0)
            c0++;
        else
            c5++;
    }
 
    // The number of 5s that can be used
    c5 = floor(c5 / 9) * 9;
    if (c0 == 0) // The number can't be
        cout << -1; // made multiple of 10
    else if (c5 == 0) // The only multiple of 90
        cout << 0; // that can be made is 0
    else {
        for (i = 0; i < c5; i++)
            cout << 5;
        for (i = 0; i < c0; i++)
            cout << 0;
    }
}
 
// Driver Code
int main()
{
    int a[] = { 5, 5, 5, 5, 5, 5, 5, 5, 0, 5, 5 };
 
    int n = sizeof(a) / sizeof(a[0]);
 
    printLargestDivisible(n, a);
 
    return 0;
}

Java




// Java program to find largest number
// divisible by 90 that can be made
// using 0 and 5
 
import java.io.*;
 
class GFG {
 
// Function to find largest number
// divisible by 90 that can be made
// using 0 and 5
static void printLargestDivisible(int n, int a[])
{
    // Count of 0s and 5s
    int i, c0 = 0, c5 = 0;
    for (i = 0; i < n; i++) {
        if (a[i] == 0)
            c0++;
        else
            c5++;
    }
 
    // The number of 5s that can be used
    c5 = (int)Math.floor(c5 / 9) * 9;
    if (c0 == 0) // The number can't be
        System.out.print(-1); // made multiple of 10
    else if (c5 == 0) // The only multiple of 90
        System.out.println(0); // that can be made is 0
    else {
        for (i = 0; i < c5; i++)
            System.out.print(5);
        for (i = 0; i < c0; i++)
            System.out.print(0);
    }
}
 
// Driver Code
 
    public static void main (String[] args) {
        int a[] = { 5, 5, 5, 5, 5, 5, 5, 5, 0, 5, 5 };
 
    int n = a.length;
 
    printLargestDivisible(n, a);
    }
}
// This code is contributed
// by shs

Python3




# Python3 program to find largest number
# divisible by 90 that can be made
# using 0 and 5
 
# from math import every methods
from math import *
 
# Function to find largest number
# divisible by 90 that can be made
# using 0 and 5
def printLargestDivisible(n, a) :
 
    # Count of 0s and 5s
    c0, c5 = 0, 0
 
    for i in range(n) :
 
        if a[i] == 0 :
            c0 += 1
        else :
            c5 += 1
 
    # The number of 5s that can be used
    c5 = floor(c5 / 9) * 9
 
    if c0 == 0 : # The number can't be
        print(-1,end = "") # made multiple of 10
 
    elif c5 == 0 : # The only multiple of 90
        print(0,end = "") # that can be made is 0
 
    else :
 
        for i in range(c5) :
            print(5,end = "")
        for i in range(c0) :
            print(0, end = "")
 
 
 
# Driver code
if __name__ == "__main__" :
 
    a = [ 5, 5, 5, 5, 5, 5, 5, 5, 0, 5, 5]
    n = len(a)
 
    # Function calling
    printLargestDivisible(n, a)
 
# This code is contributed by
# ANKITRAI1

C#




// C# program to find largest number
// divisible by 90 that can be made
// using 0 and 5
using System;
 
class GFG {
 
// Function to find largest number
// divisible by 90 that can be made
// using 0 and 5
public static void printLargestDivisible(int n,
                                       int[] a)
{
     
    // Count of 0s and 5s
    int i, c0 = 0, c5 = 0;
    for (i = 0; i < n; i++)
    {
        if (a[i] == 0)
        {
            c0++;
        }
        else
        {
            c5++;
        }
    }
     
    // The number of 5s that can be used
    c5 = (c5 / 9) * 9;
     
    // The number can't be
    if (c0 == 0)
    {
         
        // made multiple of 10
        Console.Write(-1);
    }
     
    // The only multiple of 90
    else if (c5 == 0)
    {
         
        // that can be made is 0
        Console.WriteLine(0);
    }
    else
    {
        for (i = 0; i < c5; i++)
        {
            Console.Write(5);
        }
        for (i = 0; i < c0; i++)
        {
            Console.Write(0);
        }
    }
}
 
    // Driver Code
    public static void Main(string[] args)
    {
        int[] a = new int[] {5, 5, 5, 5, 5,
                         5, 5, 5, 0, 5, 5};
        int n = a.Length;
        printLargestDivisible(n, a);
    }
}
 
// This code is contributed by Shrikant13

PHP




<?php
// PHP program to find largest number
// divisible by 90 that can be made
// using 0 and 5
 
// Function to find largest number
// divisible by 90 that can be made
// using 0 and 5
function printLargestDivisible($n, $a)
{
    // Count of 0s and 5s
    $i;
    $c0 = 0;
    $c5 = 0;
    for ($i = 0; $i < $n; $i++)
    {
        if ($a[$i] == 0)
            $c0++;
        else
            $c5++;
    }
 
    // The number of 5s that can be used
    $c5 = floor($c5 / 9) * 9;
    if ($c0 == 0) // The number can't be
        echo -1;  // made multiple of 10
    else if ($c5 == 0) // The only multiple of 90
        echo 0;        // that can be made is 0
    else
    {
        for ($i = 0; $i < $c5; $i++)
            echo 5;
        for ($i = 0; $i < $c0; $i++)
            echo 0;
    }
}
 
// Driver Code
$a = array( 5, 5, 5, 5, 5, 5,
            5, 5, 0, 5, 5 );
 
$n = sizeof($a);
 
printLargestDivisible($n, $a);
 
// This code is contributed by ajit
?>

Javascript




<script>
    // Javascript program to find largest number
    // divisible by 90 that can be made
    // using 0 and 5
     
    // Function to find largest number
    // divisible by 90 that can be made
    // using 0 and 5
    function printLargestDivisible(n, a)
    {
 
        // Count of 0s and 5s
        let i, c0 = 0, c5 = 0;
        for (i = 0; i < n; i++)
        {
            if (a[i] == 0)
            {
                c0++;
            }
            else
            {
                c5++;
            }
        }
 
        // The number of 5s that can be used
        c5 = parseInt(c5 / 9, 10) * 9;
 
        // The number can't be
        if (c0 == 0)
        {
 
            // made multiple of 10
            document.write(-1);
        }
 
        // The only multiple of 90
        else if (c5 == 0)
        {
 
            // that can be made is 0
            document.write(0 + "</br>");
        }
        else
        {
            for (i = 0; i < c5; i++)
            {
                document.write(5);
            }
            for (i = 0; i < c0; i++)
            {
                document.write(0);
            }
        }
    }
     
    let a = [5, 5, 5, 5, 5, 5, 5, 5, 0, 5, 5];
    let n = a.length;
    printLargestDivisible(n, a);
     
</script>
Output: 
5555555550

 

Time complexity: O(N), where N is the number of elements in the array.
 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :