📌  相关文章
📜  由不完整数字组成的最长子序列的长度

📅  最后修改于: 2021-05-17 18:37:34             🧑  作者: Mango

给定一个由N个自然数组成的数组arr [] ,任务是从不包含任何不足数的数组中找到最长子序列的长度。

例子:

方法:解决问题的想法是简单地计算数组中存在的不足数字的数量。所有剩余数组元素的计数将是最长子序列所需的长度。请按照以下步骤解决问题:

  • 初始化一个变量,例如res ,以存储非不足的数组元素的数量。
  • 遍历数组arr []。对于每个数组元素,请检查其是否为不足的数字。
  • 计算当前数组元素所有除数的总和如果总和小于2 * arr [i] 则返回true 。否则,返回false
  • 如果发现数组元素为假,则增加res
  • 完成上述步骤后,打印 res的值作为必需的答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if n is
// a deficient number or not
bool isNonDeficient(int n)
{
    // Stores sum of divisors
    int sum = 0;
 
    // Iterate over the range [1, sqrt(N)]
    for (int i = 1; i <= sqrt(n); i++) {
 
        // If n is divisible by i
        if (n % i == 0) {
 
            // If divisors are equal,
            // add only one of them
            if (n / i == i) {
                sum = sum + i;
            }
 
            // Otherwise add both
            else {
                sum = sum + i;
                sum = sum + (n / i);
            }
        }
    }
    return sum >= 2 * n;
}
 
// Function to print the longest
// subsequence which does not
// contain any deficient numbers
int LongestNonDeficientSubsequence(int arr[], int n)
{
    // Stores the count of array
    // elements which are non-deficient
    int res = 0;
 
    // Traverse the array
    for (int i = 0; i < n; i++) {
 
        // If element is non-deficient
        if (isNonDeficient(arr[i])) {
            res += 1;
        }
    }
 
    // Return the answer
    return res;
}
 
// Driver Code
int main()
{
    int arr[]
        = { 13, 55, 240, 32, 24, 27,
            56, 80, 100, 330, 89 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << LongestNonDeficientSubsequence(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to check if n is
// a deficient number or not
static boolean isNonDeficient(int n)
{
     
    // Stores sum of divisors
    int sum = 0;
 
    // Iterate over the range [1, sqrt(N)]
    for(int i = 1; i <= Math.sqrt(n); i++)
    {
         
        // If n is divisible by i
        if (n % i == 0)
        {
             
            // If divisors are equal,
            // add only one of them
            if (n / i == i)
            {
                sum = sum + i;
            }
 
            // Otherwise add both
            else
            {
                sum = sum + i;
                sum = sum + (n / i);
            }
        }
    }
    return sum >= 2 * n;
}
 
// Function to print the longest
// subsequence which does not
// contain any deficient numbers
static int LongestNonDeficientSubsequence(int arr[],
                                          int n)
{
     
    // Stores the count of array
    // elements which are non-deficient
    int res = 0;
 
    // Traverse the array
    for(int i = 0; i < n; i++)
    {
         
        // If element is non-deficient
        if (isNonDeficient(arr[i]))
        {
            res += 1;
        }
    }
     
    // Return the answer
    return res;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 13, 55, 240, 32, 24, 27,
                  56, 80, 100, 330, 89 };
    int N = arr.length;
 
    System.out.print(
        LongestNonDeficientSubsequence(arr, N));
}
}
 
// This code is contributed by splevel62


Python3
# Python3 program for the above approach
import math
 
# Function to check if n is
# a deficient number or not
def isNonDeficient(n):
     
    # Stores sum of divisors
    sum = 0
     
    # Iterate over the range [1, sqrt(N)]
    for i in range(1, int(math.sqrt(n)) + 1):
         
        # If n is divisible by i
        if (n % i == 0):
 
            # If divisors are equal,
            # add only one of them
            if (n // i == i):
                sum = sum + i
 
            # Otherwise add both
            else:
                sum = sum + i
                sum = sum + (n // i)
 
    return sum >= 2 * n
 
# Function to print the longest
# subsequence which does not
# contain any deficient numbers
def LongestNonDeficientSubsequence(arr, n):
     
    # Stores the count of array
    # elements which are non-deficient
    res = 0
 
    # Traverse the array
    for i in range(n):
 
        # If element is non-deficient
        if (isNonDeficient(arr[i])):
            res += 1
 
    # Return the answer
    return res
 
# Driver Code
if __name__ == "__main__":
     
    arr = [ 13, 55, 240, 32, 24, 27,
            56, 80, 100, 330, 89 ]
    N = len(arr)
 
    print(LongestNonDeficientSubsequence(arr, N))
 
# This code is contributed by chitranayal


C#
// C# program for above approach
using System;
 
public class GFG
{
 
  // Function to check if n is
  // a deficient number or not
  static bool isNonDeficient(int n)
  {
 
    // Stores sum of divisors
    int sum = 0;
 
    // Iterate over the range [1, sqrt(N)]
    for(int i = 1; i <= Math.Sqrt(n); i++)
    {
 
      // If n is divisible by i
      if (n % i == 0)
      {
 
        // If divisors are equal,
        // add only one of them
        if (n / i == i)
        {
          sum = sum + i;
        }
 
        // Otherwise add both
        else
        {
          sum = sum + i;
          sum = sum + (n / i);
        }
      }
    }
    return sum >= 2 * n;
  }
 
  // Function to print the longest
  // subsequence which does not
  // contain any deficient numbers
  static int LongestNonDeficientSubsequence(int[] arr,
                                            int n)
  {
 
    // Stores the count of array
    // elements which are non-deficient
    int res = 0;
 
    // Traverse the array
    for(int i = 0; i < n; i++)
    {
 
      // If element is non-deficient
      if (isNonDeficient(arr[i]))
      {
        res += 1;
      }
    }
 
    // Return the answer
    return res;
  }
 
 
  // Driver code
  public static void Main(String[] args)
  {
    int[] arr = { 13, 55, 240, 32, 24, 27,
                 56, 80, 100, 330, 89 };
    int N = arr.Length;
 
    Console.WriteLine(
      LongestNonDeficientSubsequence(arr, N));
  }
}
 
// This code is contributed by splevel62.


输出:
6

时间复杂度: O(N 3/2 )
辅助空间: O(N)