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 which 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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` `?> ` |

*chevron_right*

*filter_none*

**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 DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Largest number made up of X and Y with count of X divisible by Y and of Y by X
- Number of words that can be made using exactly P consonants and Q vowels from the given string
- Check if X and Y can be made zero by using given operation any number of times
- Check if N and M can be made equal by increasing N by A and decreasing M by B
- Find two numbers made up of a given digit such that their difference is divisible by N
- Total number of different staircase that can made from N boxes
- Check whether the number can be made perfect square after adding 1
- Check if given array can be made 0 with given operations performed any number of times
- Check whether the number can be made perfect square after adding K
- Check whether the number can be made palindromic after adding K
- Check if number can be made prime by deleting a single digit
- Check whether an Array can be made 0 by splitting and merging repeatedly
- Check if elements of array can be made equal by multiplying given prime numbers
- Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts
- Queries to check whether all the elements can be made positive by flipping signs exactly K times
- Check if all elements of the given array can be made 0 by decrementing value in pairs
- Check if given intervals can be made non-overlapping by adding/subtracting some X
- Check if all elements of a Circular Array can be made equal by increments of adjacent pairs
- Largest number with the given set of N digits that is divisible by 2, 3 and 5
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]

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.