Related Articles

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

• Last Updated : 24 Mar, 2021

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:

• Traverse the given array and for each element in the array and check if it is a Nude number or not.
• If the element is a Nude Number, it will be included in the resultant longest subsequence. Hence increment the count of elements in the subsequence by 1.
• Print the value of count after the above steps.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``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);` `    ``// Function Call``    ``cout << longestNudeSubseq(arr, n)``         ``<< endl;``    ``return` `0;``}`

## Java

 `// 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

 `# 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#

 `// 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`

## Javascript

 ``
Output:
`4`

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

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