📌  相关文章
📜  数组中最长子序列的长度,所有元素都为裸数字

📅  最后修改于: 2021-09-06 06:17:59             🧑  作者: Mango

给定一个由N 个正整数组成的数组arr[] ,任务是打印数组的最长子序列的长度,使其所有元素都是裸数。

例子:

解决方法:按照以下步骤解决问题:

  • 遍历给定的数组和数组中的每个元素,并检查它是否为 Nude 数字。
  • 如果元素是Nude Number ,它将包含在生成的最长子序列中。因此,将子序列中元素的计数增加1
  • 完成上述步骤后打印count的值。

下面是上述方法的实现:

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[0]);
 
    // 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


输出:
4

时间复杂度: O(N*log 10 N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live