Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Practice Mathematical Algorithm
  • Mathematical Algorithms
  • Pythagorean Triplet
  • Fibonacci Number
  • Euclidean Algorithm
  • LCM of Array
  • GCD of Array
  • Binomial Coefficient
  • Catalan Numbers
  • Sieve of Eratosthenes
  • Euler Totient Function
  • Modular Exponentiation
  • Modular Multiplicative Inverse
  • Stein's Algorithm
  • Juggler Sequence
  • Chinese Remainder Theorem
  • Quiz on Fibonacci Numbers
Open In App
Next Article:
Largest number divisible by 90 that can be made using 0 and 5
Next article icon

Largest number divisible by 90 that can be made using 0 and 5

Last Updated : 28 Jan, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 : 5555555550Input : 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 
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> 
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  ?> 

Output
5555555550 

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

Auxiliary Space: O(1)
 


Next Article
Largest number divisible by 90 that can be made using 0 and 5

A

Abdullah Aslam
Improve
Article Tags :
  • Mathematical
  • DSA
  • divisibility
  • number-digits
Practice Tags :
  • Mathematical

Similar Reads

    Largest number made up of X and Y with count of X divisible by Y and of Y by X
    Given three integers X, Y and N, the task is to find the largest number possible of length N consisting only of X and Y as its digits, such that, the count of X's in it is divisible by Y and vice-versa. If no such number can be formed, print -1.Examples: Input: N = 3, X = 5, Y = 3 Output: 555 Explan
    6 min read
    Largest number with the given set of N digits that is divisible by 2, 3 and 5
    Given a set of 'N' digits. The task is to find the maximum integer that we can make from these digits. The resultant number must be divisible by 2, 3, and 5. Note: It is not necessary to use all the digits from the set. Also, leading zeroes are not allowed.Examples: Input: N = 11, setOfDigits = {3,
    15+ min read
    Check if a large number is divisible by 2, 3 and 5 or not
    Given a number, the task is to check if a number is divisible by 2, 3, and 5 or not. The input number may be large and it may not be possible to store even if we use long long int, so the number is taken as a string.Examples: Input : str = "725" Output : NO Input : str = "263730746028908374890" Outp
    11 min read
    Possible to make a divisible by 3 number using all digits in an array
    Given an array of integers, the task is to find whether it's possible to construct an integer using all the digits of these numbers such that it would be divisible by 3. If it is possible then print "Yes" and if not print "No". Examples: Input : arr[] = {40, 50, 90} Output : Yes We can construct a n
    4 min read
    Check if a large number is divisible by 5 or not
    Given a number, the task is to check if number is divisible by 5. The input number may be large and it may not be possible to store even if we use long long int. Examples: Input : n = 56945255 Output : Yes Input : n = 1234567589333150 Output : Yes Input : n = 3635883959606670431112222 Output : NoRec
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences