Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmMaximum length of a sub-array with ugly numbers

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 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 is {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 <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, then 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;
}


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<Integer> 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, then 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, then 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[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<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, then 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




<script>
 
// javascript implementation of the approach
 
   
// Function to get the nth ugly number
function uglyNumber( n)
{
    // To store ugly numbers
    var ugly = [];
    var i2 = 0, i3 = 0, i5 = 0;
    var next_multiple_of_2 = 2;
    var next_multiple_of_3 = 3;
    var next_multiple_of_5 = 5;
    var next_ugly_no = 1;
   
    ugly[0] = 1;
    for (var 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
function maxUglySubarray(arr,  n)
{
    var s = []
    var i = 1;
   
    // Insert ugly numbers in set
    // which are less than 1000
    while (true)
    {
        var next_ugly_number = uglyNumber(i);
        if (next_ugly_number > 1000)
            break;
        s.push(next_ugly_number);
        i++;
    }
   
    var current_max = 0, max_so_far = 0;
   
    for (i = 0; i < n; i++)
    {
   
        // Check if element is non ugly
        if (!s.includes(arr[i]))
            current_max = 0;
   
        // If element is ugly, then 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
 
    var arr = [ 1, 0, 6, 7, 320, 800, 100, 648 ];
    var n = arr.length;
    document.write(maxUglySubarray(arr, n));
     
</script>


Output: 

4

 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Calisto Chipfumbu
Calisto Chipfumbuhttp://cchipfumbu@gmail.com
I have 5 years' worth of experience in the IT industry, primarily focused on Linux and Database administration. In those years, apart from learning significant technical knowledge, I also became comfortable working in a professional team and adapting to my environment, as I switched through 3 roles in that time.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments