Open In App

Length of longest subsequence in an Array having all elements as Nude Numbers

Given an array arr[] of N positive integers, the task is to print the length of the longest subsequence of the array such that all of its elements are Nude Numbers.

Examples:

Input: arr[] = {34, 34, 2, 2, 3, 333, 221, 32 }
Output: 4
Explanation:
Longest Nude number subsequence is {2, 2, 3, 333} and hence the answer is 4.
Input: arr[] = {456, 44, 104, 133, 39, 325  }
Output: 1
Explanation:
Longest Nude number subsequence is {44} and hence the answer is 1.

Approach: To solve the problem follow the steps given below:

Below is the implementation of the above approach:

// C++ program for the above approach
#include <bits/stdc++.h> 
using namespace std;

// Function to check if the number
// is a Nude number
bool isNudeNum(int n)
{
    // Variable initialization
    int copy, length, flag = 0;
    copy = n;
    string temp;

    // Integer 'copy' is converted
    // to a string
    temp = to_string(copy);

    // Total digits in the number
    length = temp.length();

    // Loop through all digits and check
    // if every digit divides n or not
    for (int i = 0; i < length; i++) {

        int num = temp[i] - '0'; 

        if (num == 0 or n % num != 0) {

            // flag is used to keep check
            flag = 1;
        }
    }

    // Return true or false as per
    // the condition
    if (flag == 1)
        return false;

    else
        return true;
}

// Function to find the longest subsequence
// which contain all Nude numbers
int longestNudeSubseq(int arr[], int n)
{
    int answer = 0;

    // Find the length of longest
    // Nude number subsequence
    for (int i = 0; i < n; i++) {
        if (isNudeNum(arr[i]))
            answer++;
    }
    return answer;
}

// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 34, 34, 2, 2, 3,
                  333, 221, 32 };

    int n = sizeof(arr) / sizeof(arr[0]);

    // Function Call
    cout << longestNudeSubseq(arr, n)
         << endl;
    return 0;
}
// Java program for the above approach
class GFG{

// Function to check if the number
// is a Nude number
static boolean isNudeNum(int n)
{
    
    // Variable initialization
    int copy, length, flag = 0;
    copy = n;
    String temp;

    // Integer 'copy' is converted
    // to a String
    temp = String.valueOf(copy);

    // Total digits in the number
    length = temp.length();

    // Loop through all digits and check
    // if every digit divides n or not
    for(int i = 0; i < length; i++)
    {
        int num = temp.charAt(i) - '0';

        if (num == 0 || n % num != 0)
        {
            
            // flag is used to keep check
            flag = 1;
        }
    }

    // Return true or false as per
    // the condition
    if (flag == 1)
        return false;
    else
        return true;
}

// Function to find the longest subsequence
// which contain all Nude numbers
static int longestNudeSubseq(int arr[], int n)
{
    int answer = 0;

    // Find the length of longest
    // Nude number subsequence
    for(int i = 0; i < n; i++) 
    {
        if (isNudeNum(arr[i]))
            answer++;
    }
    return answer;
}

// Driver Code
public static void main(String[] args)
{
    
    // Given array arr[]
    int arr[] = { 34, 34, 2, 2, 3,
                  333, 221, 32 };

    int n = arr.length;

    // Function call
    System.out.print(longestNudeSubseq(arr, n) + "\n");
}
}

// This code is contributed by PrinciRaj1992
# Python3 program for the above approach

# Function to check if the number 
# is a Nude number 
def isNudeNum(n):
    
    # Variable initialization 
    flag = 0
    copy = n 

    # Integer 'copy' is converted 
    # to a string 
    temp = str(copy) 

    # Total digits in the number 
    length = len(temp)

    # Loop through all digits and check 
    # if every digit divides n or not 
    for i in range(length): 
        num = ord(temp[i]) - ord('0') 

        if ((num == 0) or (n % num != 0)): 

            # flag is used to keep check 
            flag = 1
        
    # Return true or false as per 
    # the condition 
    if (flag == 1):
        return False
    else:
        return True

# Function to find the longest subsequence 
# which contain all Nude numbers 
def longestNudeSubseq(arr, n): 
    
    answer = 0

    # Find the length of longest 
    # Nude number subsequence 
    for i in range(n): 
        if (isNudeNum(arr[i])):
            answer += 1
    
    return answer 

# Driver Code 

# Given array arr[] 
arr = [ 34, 34, 2, 2, 3, 
        333, 221, 32 ] 

n = len(arr) 

# Function call 
print(longestNudeSubseq(arr, n))

# This code is contributed by sanjoy_62
// C# program for the above approach
using System;

class GFG{

// Function to check if the number
// is a Nude number
static bool isNudeNum(int n)
{
    
    // Variable initialization
    int copy, length, flag = 0;
    copy = n;
    String temp;

    // int 'copy' is converted
    // to a String
    temp = String.Join("", copy);

    // Total digits in the number
    length = temp.Length;

    // Loop through all digits and check
    // if every digit divides n or not
    for(int i = 0; i < length; i++)
    {
        int num = temp[i] - '0';

        if (num == 0 || n % num != 0)
        {
            
            // flag is used to keep check
            flag = 1;
        }
    }

    // Return true or false as per
    // the condition
    if (flag == 1)
        return false;
    else
        return true;
}

// Function to find the longest subsequence
// which contain all Nude numbers
static int longestNudeSubseq(int []arr, int n)
{
    int answer = 0;

    // Find the length of longest
    // Nude number subsequence
    for(int i = 0; i < n; i++) 
    {
        if (isNudeNum(arr[i]))
            answer++;
    }
    return answer;
}

// Driver Code
public static void Main(String[] args)
{
    
    // Given array []arr
    int []arr = { 34, 34, 2, 2, 3,
                  333, 221, 32 };

    int n = arr.Length;

    // Function call
    Console.Write(longestNudeSubseq(arr, n) + "\n");
}
}

// This code is contributed by amal kumar choubey
 
<script>
    // Javascript program for the above approach 
    
    // Function to check if the number 
    // is a Nude number 
    function isNudeNum(n) 
    { 
        // Variable initialization 
        let copy, length, flag = 0; 
        copy = n; 
        let temp; 

        // Integer 'copy' is converted 
        // to a string 
        temp = copy.toString(); 

        // Total digits in the number 
        length = temp.length; 

        // Loop through all digits and check 
        // if every digit divides n or not 
        for (let i = 0; i < length; i++) { 

            let num = temp[i].charCodeAt() - '0'.charCodeAt();  

            if (num == 0 || n % num != 0) { 

                // flag is used to keep check 
                flag = 1; 
            } 
        } 

        // Return true or false as per 
        // the condition 
        if (flag == 1) 
            return false; 

        else
            return true; 
    } 

    // Function to find the longest subsequence 
    // which contain all Nude numbers 
    function longestNudeSubseq(arr, n) 
    { 
        let answer = 0; 

        // Find the length of longest 
        // Nude number subsequence 
        for (let i = 0; i < n; i++) { 
            if (isNudeNum(arr[i])) 
                answer++; 
        } 
        return answer; 
    } 

    // Given array arr[] 
    let arr = [ 34, 34, 2, 2, 3, 
                  333, 221, 32 ]; 
  
    let n = arr.length; 
  
    // Function Call 
    document.write(longestNudeSubseq(arr, n));
    
    // This code is contributed by divyesh072019.
</script>

Output
4

Time Complexity: O(N*log10N)
Auxiliary Space: O(1)

Approach : Using Set or Hashing

To solve the problem follow the steps given below:

Below is the implementation of the above approach:

def is_nude_number(num):
    # Define a set of Nude Numbers
    nude_numbers = {2, 3, 5, 7}

    # Check if the number is a Nude Number
    while num > 0:
        digit = num % 10
        if digit not in nude_numbers:
            return False
        num //= 10
    return True

def longest_nude_numbers_subsequence(arr):
    max_length = 0
    current_length = 0

    for num in arr:
        if is_nude_number(num):
            current_length += 1
            max_length = max(max_length, current_length)
        else:
            current_length = 0

    return max_length

# Example usage:
arr1 = [34, 34, 2, 2, 3, 333, 221, 32]

print(longest_nude_numbers_subsequence(arr1))

Output
4

Time Complexity: O(N * log(num)), where N is the number of elements in the array.

Auxiliary Space: O(1)









Article Tags :