Length of longest subsequence in an Array having all elements as Nude Numbers | C ++ program for the above approach ; Function to check if the number is a Nude number ; Variable initialization ; Integer ' copy ' is converted to a string ; Total digits in the number ; Loop through all digits and check if every digit divides n or not ; flag is used to keep check ; Return true or false as per the condition ; Function to find the longest subsequence which contain all Nude numbers ; Find the length of longest Nude number subsequence ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isNudeNum ( int n ) { int copy , length , flag = 0 ; copy = n ; string temp ; temp = to_string ( copy ) ; length = temp . length ( ) ; for ( int i = 0 ; i < length ; i ++ ) { int num = temp [ i ] - '0' ; if ( num == 0 or n % num != 0 ) { flag = 1 ; } } if ( flag == 1 ) return false ; else return true ; } int longestNudeSubseq ( int arr [ ] , int n ) { int answer = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isNudeNum ( arr [ i ] ) ) answer ++ ; } return answer ; } int main ( ) { int arr [ ] = { 34 , 34 , 2 , 2 , 3 , 333 , 221 , 32 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << longestNudeSubseq ( arr , n ) << endl ; return 0 ; }