Largest number divisible by 90 that can be made using 0 and 5
Last Updated :
28 Jan, 2024
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++
#include <bits/stdc++.h>
using namespace std;
void printLargestDivisible( int n, int a[])
{
int i, c0 = 0, c5 = 0;
for (i = 0; i < n; i++) {
if (a[i] == 0)
c0++;
else
c5++;
}
c5 = floor (c5 / 9) * 9;
if (c0 == 0)
cout << -1;
else if (c5 == 0)
cout << 0;
else {
for (i = 0; i < c5; i++)
cout << 5;
for (i = 0; i < c0; i++)
cout << 0;
}
}
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
import java.io.*;
class GFG {
static void printLargestDivisible( int n, int a[])
{
int i, c0 = 0 , c5 = 0 ;
for (i = 0 ; i < n; i++) {
if (a[i] == 0 )
c0++;
else
c5++;
}
c5 = ( int )Math.floor(c5 / 9 ) * 9 ;
if (c0 == 0 )
System.out.print(- 1 );
else if (c5 == 0 )
System.out.println( 0 );
else {
for (i = 0 ; i < c5; i++)
System.out.print( 5 );
for (i = 0 ; i < c0; i++)
System.out.print( 0 );
}
}
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);
}
}
|
Python3
from math import *
def printLargestDivisible(n, a) :
c0, c5 = 0 , 0
for i in range (n) :
if a[i] = = 0 :
c0 + = 1
else :
c5 + = 1
c5 = floor(c5 / 9 ) * 9
if c0 = = 0 :
print ( - 1 ,end = "")
elif c5 = = 0 :
print ( 0 ,end = "")
else :
for i in range (c5) :
print ( 5 ,end = "")
for i in range (c0) :
print ( 0 , end = "")
if __name__ = = "__main__" :
a = [ 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 0 , 5 , 5 ]
n = len (a)
printLargestDivisible(n, a)
|
C#
using System;
class GFG {
public static void printLargestDivisible( int n,
int [] a)
{
int i, c0 = 0, c5 = 0;
for (i = 0; i < n; i++)
{
if (a[i] == 0)
{
c0++;
}
else
{
c5++;
}
}
c5 = (c5 / 9) * 9;
if (c0 == 0)
{
Console.Write(-1);
}
else if (c5 == 0)
{
Console.WriteLine(0);
}
else
{
for (i = 0; i < c5; i++)
{
Console.Write(5);
}
for (i = 0; i < c0; i++)
{
Console.Write(0);
}
}
}
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);
}
}
|
Javascript
<script>
function printLargestDivisible(n, a)
{
let i, c0 = 0, c5 = 0;
for (i = 0; i < n; i++)
{
if (a[i] == 0)
{
c0++;
}
else
{
c5++;
}
}
c5 = parseInt(c5 / 9, 10) * 9;
if (c0 == 0)
{
document.write(-1);
}
else if (c5 == 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>
|
PHP
<?php
function printLargestDivisible( $n , $a )
{
$i ;
$c0 = 0;
$c5 = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $a [ $i ] == 0)
$c0 ++;
else
$c5 ++;
}
$c5 = floor ( $c5 / 9) * 9;
if ( $c0 == 0)
echo -1;
else if ( $c5 == 0)
echo 0;
else
{
for ( $i = 0; $i < $c5 ; $i ++)
echo 5;
for ( $i = 0; $i < $c0 ; $i ++)
echo 0;
}
}
$a = array ( 5, 5, 5, 5, 5, 5,
5, 5, 0, 5, 5 );
$n = sizeof( $a );
printLargestDivisible( $n , $a );
?>
|
Time complexity: O(N), where N is the number of elements in the array.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...