Related Articles

# Maximum length of a sub-array with ugly numbers

• Difficulty Level : Medium
• Last Updated : 14 May, 2021

Given an array arr[] of N elements (0 ≤ arr[i] ≤ 1000). The task is to find the maximum length of the sub-array that contains only ugly numbers. Ugly numbers are numbers whose only prime factors are 2, 3, or 5
The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ….. shows the first few ugly numbers. By convention, 1 is included.

Examples:

Input: arr[] = {1, 2, 7, 9, 120, 810, 374}
Output:
The Longest possible sub-array of ugly number sis {9, 120, 810}
Input: arr[] = {109, 480, 320, 142, 121, 1}
Output: 2

Approach:

• Take a unordered_set, and insert all the ugly numbers which are less than 1000 in the set.
• Traverse the array with two variables named current_max and max_so_far.
• Check for each element if it is present in the set.
• If an ugly number is found then increment current_max and compare it with max_so_far.
• If current_max > max_so_far then max_so_far = current_max.
• Every time a non-ugly element is found, reset current_max = 0.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `// Function to get the nth ugly number``unsigned uglyNumber(``int` `n)``{``    ``// To store ugly numbers``    ``int` `ugly[n];``    ``int` `i2 = 0, i3 = 0, i5 = 0;``    ``int` `next_multiple_of_2 = 2;``    ``int` `next_multiple_of_3 = 3;``    ``int` `next_multiple_of_5 = 5;``    ``int` `next_ugly_no = 1;` `    ``ugly = 1;``    ``for` `(``int` `i = 1; i < n; i++) {``        ``next_ugly_no = min(next_multiple_of_2,``                           ``min(next_multiple_of_3,``                               ``next_multiple_of_5));``        ``ugly[i] = next_ugly_no;``        ``if` `(next_ugly_no == next_multiple_of_2) {``            ``i2 = i2 + 1;``            ``next_multiple_of_2 = ugly[i2] * 2;``        ``}``        ``if` `(next_ugly_no == next_multiple_of_3) {``            ``i3 = i3 + 1;``            ``next_multiple_of_3 = ugly[i3] * 3;``        ``}``        ``if` `(next_ugly_no == next_multiple_of_5) {``            ``i5 = i5 + 1;``            ``next_multiple_of_5 = ugly[i5] * 5;``        ``}``    ``}` `    ``return` `next_ugly_no;``}` `// Function to return the length of the``// maximum sub-array of ugly numbers``int` `maxUglySubarray(``int` `arr[], ``int` `n)``{``    ``unordered_set<``int``> s;``    ``int` `i = 1;` `    ``// Insert ugly numbers in set``    ``// which are less than 1000``    ``while` `(1) {``        ``int` `next_ugly_number = uglyNumber(i);``        ``if` `(next_ugly_number > 1000)``            ``break``;``        ``s.insert(next_ugly_number);``        ``i++;``    ``}` `    ``int` `current_max = 0, max_so_far = 0;` `    ``for` `(``int` `i = 0; i < n; i++) {` `        ``// Check if element is non ugly``        ``if` `(s.find(arr[i]) == s.end())``            ``current_max = 0;` `        ``// If element is ugly, than update``        ``// current_max and max_so_far accordingly``        ``else` `{``            ``current_max++;``            ``max_so_far = max(current_max, max_so_far);``        ``}``    ``}` `    ``return` `max_so_far;``}` `// Driver code``int` `main()``{` `    ``int` `arr[] = { 1, 0, 6, 7, 320, 800, 100, 648 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr);``    ``cout << maxUglySubarray(arr, n);` `    ``return` `0;``}`

## Java

 `// Java implementation of the approach``import` `java.util.*;` `class` `GFG``{` `// Function to get the nth ugly number``static` `int` `uglyNumber(``int` `n)``{``    ``// To store ugly numbers``    ``int` `[]ugly = ``new` `int``[n];``    ``int` `i2 = ``0``, i3 = ``0``, i5 = ``0``;``    ``int` `next_multiple_of_2 = ``2``;``    ``int` `next_multiple_of_3 = ``3``;``    ``int` `next_multiple_of_5 = ``5``;``    ``int` `next_ugly_no = ``1``;` `    ``ugly[``0``] = ``1``;``    ``for` `(``int` `i = ``1``; i < n; i++)``    ``{``        ``next_ugly_no = Math.min(next_multiple_of_2,``                       ``Math.min(next_multiple_of_3,``                                ``next_multiple_of_5));``        ``ugly[i] = next_ugly_no;``        ``if` `(next_ugly_no == next_multiple_of_2)``        ``{``            ``i2 = i2 + ``1``;``            ``next_multiple_of_2 = ugly[i2] * ``2``;``        ``}``        ``if` `(next_ugly_no == next_multiple_of_3)``        ``{``            ``i3 = i3 + ``1``;``            ``next_multiple_of_3 = ugly[i3] * ``3``;``        ``}``        ``if` `(next_ugly_no == next_multiple_of_5)``        ``{``            ``i5 = i5 + ``1``;``            ``next_multiple_of_5 = ugly[i5] * ``5``;``        ``}``    ``}``    ``return` `next_ugly_no;``}` `// Function to return the length of the``// maximum sub-array of ugly numbers``static` `int` `maxUglySubarray(``int` `arr[], ``int` `n)``{``    ``HashSet s = ``new` `HashSet<>();``    ``int` `i = ``1``;` `    ``// Insert ugly numbers in set``    ``// which are less than 1000``    ``while` `(``true``)``    ``{``        ``int` `next_ugly_number = uglyNumber(i);``        ``if` `(next_ugly_number > ``1000``)``            ``break``;``        ``s.add(next_ugly_number);``        ``i++;``    ``}` `    ``int` `current_max = ``0``, max_so_far = ``0``;` `    ``for` `(i = ``0``; i < n; i++)``    ``{` `        ``// Check if element is non ugly``        ``if` `(!s.contains(arr[i]))``            ``current_max = ``0``;` `        ``// If element is ugly, than update``        ``// current_max and max_so_far accordingly``        ``else``        ``{``            ``current_max++;``            ``max_so_far = Math.max(current_max,``                                  ``max_so_far);``        ``}``    ``}``    ``return` `max_so_far;``}` `// Driver code``public` `static` `void` `main(String[] args)``{``    ``int` `arr[] = { ``1``, ``0``, ``6``, ``7``, ``320``, ``800``, ``100``, ``648` `};``    ``int` `n = arr.length;``    ``System.out.println(maxUglySubarray(arr, n));``}``}` `// This code is contributed by Rajput-Ji`

## Python3

 `# Python 3 implementation of the approach` `# Function to get the nth ugly number``def` `uglyNumber(n):``    ` `    ``# To store ugly numbers``    ``ugly ``=` `[``None` `for` `i ``in` `range``(n)]``    ``i2 ``=` `0``    ``i3 ``=` `0``    ``i5 ``=` `0``    ``next_multiple_of_2 ``=` `2``    ``next_multiple_of_3 ``=` `3``    ``next_multiple_of_5 ``=` `5``    ``next_ugly_no ``=` `1` `    ``ugly[``0``] ``=` `1``    ``for` `i ``in` `range``(``1``, n, ``1``):``        ``next_ugly_no ``=` `min``(next_multiple_of_2,``                       ``min``(next_multiple_of_3,``                           ``next_multiple_of_5))``        ``ugly[i] ``=` `next_ugly_no``        ``if` `(next_ugly_no ``=``=` `next_multiple_of_2):``            ``i2 ``=` `i2 ``+` `1``            ``next_multiple_of_2 ``=` `ugly[i2] ``*` `2``        ``if` `(next_ugly_no ``=``=` `next_multiple_of_3):``            ``i3 ``=` `i3 ``+` `1``            ``next_multiple_of_3 ``=` `ugly[i3] ``*` `3``        ``if` `(next_ugly_no ``=``=` `next_multiple_of_5):``            ``i5 ``=` `i5 ``+` `1``            ``next_multiple_of_5 ``=` `ugly[i5] ``*` `5` `    ``return` `next_ugly_no` `# Function to return the length of the``# maximum sub-array of ugly numbers``def` `maxUglySubarray(arr, n):``    ``s ``=` `set``()``    ``i ``=` `1` `    ``# Insert ugly numbers in set``    ``# which are less than 1000``    ``while` `(``1``):``        ``next_ugly_number ``=` `uglyNumber(i)``        ``if` `(next_ugly_number >``=` `1000``):``            ``break``        ``s.add(next_ugly_number)``        ``i ``+``=` `1` `    ``current_max ``=` `0``    ``max_so_far ``=` `0` `    ``for` `i ``in` `range``(n):``        ` `        ``# Check if element is non ugly``        ``if` `(arr[i] ``not` `in` `s):``            ``current_max ``=` `0` `        ``# If element is ugly, than update``        ``# current_max and max_so_far accordingly``        ``else``:``            ``current_max ``+``=` `1``            ``max_so_far ``=` `max``(current_max,``                              ``max_so_far)` `    ``return` `max_so_far` `# Driver code``if` `__name__ ``=``=` `'__main__'``:``    ``arr ``=` `[``1``, ``0``, ``6``, ``7``, ``320``, ``800``, ``100``, ``648``]``    ``n ``=` `len``(arr)``    ``print``(maxUglySubarray(arr, n))``    ` `# This code is contributed by``# Surendra_Gangwar`

## C#

 `// C# implementation of the approach``using` `System;``using` `System.Collections.Generic;``    ` `class` `GFG``{` `// Function to get the nth ugly number``static` `int` `uglyNumber(``int` `n)``{``    ``// To store ugly numbers``    ``int` `[]ugly = ``new` `int``[n];``    ``int` `i2 = 0, i3 = 0, i5 = 0;``    ``int` `next_multiple_of_2 = 2;``    ``int` `next_multiple_of_3 = 3;``    ``int` `next_multiple_of_5 = 5;``    ``int` `next_ugly_no = 1;` `    ``ugly = 1;``    ``for` `(``int` `i = 1; i < n; i++)``    ``{``        ``next_ugly_no = Math.Min(next_multiple_of_2,``                       ``Math.Min(next_multiple_of_3,``                                ``next_multiple_of_5));``        ``ugly[i] = next_ugly_no;``        ``if` `(next_ugly_no == next_multiple_of_2)``        ``{``            ``i2 = i2 + 1;``            ``next_multiple_of_2 = ugly[i2] * 2;``        ``}``        ``if` `(next_ugly_no == next_multiple_of_3)``        ``{``            ``i3 = i3 + 1;``            ``next_multiple_of_3 = ugly[i3] * 3;``        ``}``        ``if` `(next_ugly_no == next_multiple_of_5)``        ``{``            ``i5 = i5 + 1;``            ``next_multiple_of_5 = ugly[i5] * 5;``        ``}``    ``}``    ``return` `next_ugly_no;``}` `// Function to return the length of the``// maximum sub-array of ugly numbers``static` `int` `maxUglySubarray(``int` `[]arr, ``int` `n)``{``    ``HashSet<``int``> s = ``new` `HashSet<``int``>();``    ``int` `i = 1;` `    ``// Insert ugly numbers in set``    ``// which are less than 1000``    ``while` `(``true``)``    ``{``        ``int` `next_ugly_number = uglyNumber(i);``        ``if` `(next_ugly_number > 1000)``            ``break``;``        ``s.Add(next_ugly_number);``        ``i++;``    ``}` `    ``int` `current_max = 0, max_so_far = 0;` `    ``for` `(i = 0; i < n; i++)``    ``{` `        ``// Check if element is non ugly``        ``if` `(!s.Contains(arr[i]))``            ``current_max = 0;` `        ``// If element is ugly, than update``        ``// current_max and max_so_far accordingly``        ``else``        ``{``            ``current_max++;``            ``max_so_far = Math.Max(current_max,``                                  ``max_so_far);``        ``}``    ``}``    ``return` `max_so_far;``}` `// Driver code``public` `static` `void` `Main(String[] args)``{``    ``int` `[]arr = { 1, 0, 6, 7, 320, 800, 100, 648 };``    ``int` `n = arr.Length;``    ``Console.WriteLine(maxUglySubarray(arr, n));``}``}` `// This code is contributed by Princi Singh`

## Javascript

 ``
Output:
`4`

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up