Maximum length of a sub-array with ugly numbers

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 first few ugly numbers. By convention, 1 is included.

Examples:

Input: arr[] = {1, 2, 7, 9, 120, 810, 374}
Output: 3
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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
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[0] = 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[0]);
    cout << maxUglySubarray(arr, n);
  
    return 0;
}

chevron_right


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

Output:

4


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : SURENDRA_GANGWAR



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.