📜  给定数组中具有复合和的最大子集

📅  最后修改于: 2022-05-13 01:57:48.091000             🧑  作者: Mango

给定数组中具有复合和的最大子集

给定一个由n 个不同的正整数组成的数组arr[] 。找到给定数组中最大子集的长度,它总和为一个复合数并打印所需的数组(顺序或打印元素无关紧要)。

例子:

方法:通过考虑所有偶数之和为偶数(除 2 外始终是合数),然后将奇数添加到该和并检查和是否是合数,可以轻松解决给定问题。不是。请按照以下步骤解决问题:

  • 创建一个temp[]向量,首先存储所有偶数,然后存储给定数组中的奇数。
  • 创建一个变量cursum ,初始化为零,用于存储临时数组的当前和,变量maxlen = 0 ,用于存储合成和的最大长度。
  • 使用变量i遍历范围[0, n)并执行以下任务:
    • 将值temp[i]添加到变量currsum
    • 如果currrsum的值是一个合数并且currsum大于maxlen则将maxlen的值设置为i+1。
  • 执行上述步骤后,打印maxlen的值作为答案,并将数组temp[]中的第一个maxlen元素作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if the current
// sum is composite or not
bool checkComposite(int n)
{
 
    // 1 and 2 are not composite
    // number
    if (n == 1 || n == 2) {
        return false;
    }
 
    // If the number is divisible
    // by any digit except 2 and itself
    // then it's composite
    for (int i = 2; i < n; i++) {
 
        // If composite
        if (n % i == 0 && i != n) {
            return true;
        }
    }
 
    return false;
}
 
// Utility Function to find largest composite
// subset sum
void largestCompositeSum(int arr[], int n)
{
 
    // Vector to store the elements of
    // arr in order of first even then
    // odd numbers
    vector temp;
 
    // Even numbers pushed first in
    // temp array
    for (int i = 0; i < n; i++) {
 
        // Even check
        if (arr[i] % 2 == 0) {
            temp.push_back(arr[i]);
        }
    }
 
    // Odd numbers pushed
    for (int i = 0; i < n; i++) {
        // Odd check
        if (arr[i] % 2 == 1) {
            temp.push_back(arr[i]);
        }
    }
 
    // To store current sum
    int cursum = 0;
 
    // To store maximum length composite
    // sum
    int maxlen = 0;
 
    for (int i = 0; i < n; i++) {
        cursum += temp[i];
 
        // If composite then update
        // cursum
        if (checkComposite(cursum)
            && cursum > maxlen) {
            maxlen = i + 1;
        }
    }
 
    cout << maxlen << endl;
 
    // Printing the required array
    for (int i = 0; i < maxlen; i++) {
        cout << temp[i] << " ";
    }
 
    return;
}
 
// Driver Code
int main()
{
 
    int n = 3;
    int arr[3] = { 8, 1, 4 };
 
    // Function called
    largestCompositeSum(arr, n);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
  // Function to check if the current
  // sum is composite or not
  static boolean checkComposite(int n)
  {
 
    // 1 and 2 are not composite
    // number
    if (n == 1 || n == 2) {
      return false;
    }
 
    // If the number is divisible
    // by any digit except 2 and itself
    // then it's composite
    for (int i = 2; i < n; i++) {
 
      // If composite
      if (n % i == 0 && i != n) {
        return true;
      }
    }
 
    return false;
  }
 
  // Utility Function to find largest composite
  // subset sum
  static void largestCompositeSum(int arr[], int n)
  {
 
    // Vector to store the elements of
    // arr in order of first even then
    // odd numbers
    Vector temp = new Vector();
 
    // Even numbers pushed first in
    // temp array
    for (int i = 0; i < n; i++) {
 
      // Even check
      if (arr[i] % 2 == 0) {
        temp.add(arr[i]);
      }
    }
 
    // Odd numbers pushed
    for (int i = 0; i < n; i++) {
      // Odd check
      if (arr[i] % 2 == 1) {
        temp.add(arr[i]);
      }
    }
 
    // To store current sum
    int cursum = 0;
 
    // To store maximum length composite
    // sum
    int maxlen = 0;
 
    for (int i = 0; i < n; i++) {
      cursum += temp.get(i);
 
      // If composite then update
      // cursum
      if (checkComposite(cursum)
          && cursum > maxlen) {
        maxlen = i + 1;
      }
    }
 
    System.out.print(maxlen +"\n");
 
    // Printing the required array
    for (int i = 0; i < maxlen; i++) {
      System.out.print(temp.get(i)+ " ");
    }
 
    return;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
 
    int n = 3;
    int arr[] = { 8, 1, 4 };
 
    // Function called
    largestCompositeSum(arr, n);
 
  }
}
 
// This code is contributed by shikhasingrajput


Python
# Python program for the above approach
 
# Function to check if the current
# sum is composite or not
def checkComposite(n):
 
    # 1 and 2 are not composite
    # number
    if (n == 1 or n == 2):
        return false
 
    # If the number is divisible
    # by any digit except 2 and itself
    # then it's composite
    for i in range(2, n):
 
        # If composite
        if (n % i == 0 and i != n):
            return True
 
    return False
 
# Utility Function to find largest composite
# subset sum
def largestCompositeSum(arr, n):
 
    # Vector to store the elements of
    # arr in order of first even then
    # odd numbers
    temp = []
 
    # Even numbers pushed first in
    # temp array
    for i in range(n):
 
        # Even check
        if (arr[i] % 2 == 0):
            temp.append(arr[i])
     
    # Odd numbers pushed
    for i in range(n):
        # Odd check
        if (arr[i] % 2 == 1):
            temp.append(arr[i])
 
    # To store current sum
    cursum = 0;
 
    # To store maximum length composite
    # sum
    maxlen = 0;
 
    for i in range(n):
        cursum += temp[i]
 
        # If composite then update
        # cursum
        if (checkComposite(cursum)
            and cursum > maxlen):
            maxlen = i + 1
 
    print(maxlen)
     
    l = len(temp) - maxlen
    for i in range(l):
        temp.remove(temp[i + maxlen])
         
    # Printing the required array
    print(*temp)
     
    return
 
# Driver code
n = 3
arr = [8, 1, 4]
 
# Function called
largestCompositeSum(arr, n);
 
# This code is contributed by Samim Hossain Mondal.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
   
    // Function to check if the current
    // sum is composite or not
    static bool checkComposite(int n)
    {
 
        // 1 and 2 are not composite
        // number
        if (n == 1 || n == 2) {
            return false;
        }
 
        // If the number is divisible
        // by any digit except 2 and itself
        // then it's composite
        for (int i = 2; i < n; i++) {
 
            // If composite
            if (n % i == 0 && i != n) {
                return true;
            }
        }
 
        return false;
    }
 
    // Utility Function to find largest composite
    // subset sum
    static void largestCompositeSum(int[] arr, int n)
    {
 
        // Vector to store the elements of
        // arr in order of first even then
        // odd numbers
        List temp = new List();
 
        // Even numbers pushed first in
        // temp array
        for (int i = 0; i < n; i++) {
 
            // Even check
            if (arr[i] % 2 == 0) {
                temp.Add(arr[i]);
            }
        }
 
        // Odd numbers pushed
        for (int i = 0; i < n; i++) {
            // Odd check
            if (arr[i] % 2 == 1) {
                temp.Add(arr[i]);
            }
        }
 
        // To store current sum
        int cursum = 0;
 
        // To store maximum length composite
        // sum
        int maxlen = 0;
 
        for (int i = 0; i < n; i++) {
            cursum += temp[i];
 
            // If composite then update
            // cursum
            if (checkComposite(cursum) && cursum > maxlen) {
                maxlen = i + 1;
            }
        }
 
        Console.WriteLine(maxlen);
 
        // Printing the required array
        for (int i = 0; i < maxlen; i++) {
            Console.Write(temp[i] + " ");
        }
 
        return;
    }
 
    // Driver Code
    public static void Main()
    {
 
        int n = 3;
        int[] arr = { 8, 1, 4 };
 
        // Function called
        largestCompositeSum(arr, n);
    }
}
 
// This code is contributed by ukasp.


Javascript


输出:
2
8 4

时间复杂度: O(n 2 )
辅助空间: O(n),临时数组需要。