📌  相关文章
📜  生成包含不同偶数且总和为 N 的最长数组

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

生成包含不同偶数且总和为 N 的最长数组

给定一个整数N 。找到包含总和为N的不同偶数的最大长度序列。如果这样的序列不存在,则返回一个空序列。

例子:

方法:这个问题可以使用贪心方法来解决。

开始将 2 中的偶数插入总和小于或等于 N 的列表中。如果不可能出现总和等于 N 的偶数子序列,只需将所有迭代后的剩余差添加到列表中的最后一个数字。

可以遵循以下方法来解决问题:

  • 如果 N 为奇数,则返回空向量,因为奇数不能表示为偶数之和。
  • 用 0 初始化一个变量(比如sum )来存储序列中元素的总和,以及一个变量(比如curr )来存储当前的偶数。
  • 直到sum的值小于 N,迭代地不断推入答案中的curr值,并不断将这些值添加到 sum 中。此外,在每次迭代时,不断将curr的值增加 2。
  • 最后,将余数(N-sum)的值添加到序列的最后一个元素。

下面是上述方法的实现:

C++
// C++ program to find Maximum length
// sequence containing distinct
// even integers with sum N
 
#include 
using namespace std;
 
// Function to find maximum
// length sequence of distinct
// positive even integers
// that adds up to N
vector maxSequence(int N)
{
    // Initializing a vector
    // to store the sequence
    vector v;
 
    // Initializing sum and curr
    // that stores the
    // sum of sequence and current
    // even integer respectively
    int curr = 2, sum = 0;
 
    // If N is odd, just return
    // the empty vector
    // as the required sequence cannot
    // be deduced for an odd integer
    if (N % 2 == 1) {
        return v;
    }
 
    // While sum of sequence remains
    // less than or equal to N, we keep
    // inserting the value of curr in
    // vector v as well as adding into sum
    // also, increment curr by 2 at each
    // iteration to get next even number
    while (sum + curr <= N) {
        v.push_back(curr);
        sum += curr;
        curr += 2;
    }
 
    int sz = v.size();
 
    // We add the difference of N and
    // sum to the last digit of sequence
    v[sz - 1] += (N - sum);
 
    // Returning the sequence
    return v;
}
 
// Driver Code
int main()
{
    int N = 12;
    vector max_sequence = maxSequence(N);
 
    // Printing the required sequence
    for (int i = 0; i < max_sequence.size(); i++) {
        cout << max_sequence[i] << " ";
    }
}


Java
// Java program to find Maximum length
// sequence containing distinct
// even integers with sum N
import java.io.*;
import java.util.*;
 
class GFG
{
 
  // Function to find maximum
  // length sequence of distinct
  // positive even integers
  // that adds up to N
  public static ArrayList maxSequence(int N)
  {
    // Initializing a arraylist
    // to store the sequence
    ArrayList v = new ArrayList();
 
    // Initializing sum and curr
    // that stores the
    // sum of sequence and current
    // even integer respectively
    int curr = 2, sum = 0;
 
    // If N is odd, just return
    // the empty arraylist
    // as the required sequence cannot
    // be deduced for an odd integer
    if (N % 2 == 1) {
      return v;
    }
 
    // While sum of sequence remains
    // less than or equal to N, we keep
    // inserting the value of curr in
    // arraylist v as well as adding into sum
    // also, increment curr by 2 at each
    // iteration to get next even number
    while (sum + curr <= N) {
      v.add(curr);
      sum += curr;
      curr += 2;
    }
 
    int sz = v.size();
 
    // We add the difference of N and
    // sum to the last digit of sequence
    v.set(sz - 1,v.get(sz-1)+(N-sum));
 
    // Returning the sequence
    return v;
  }
 
  public static void main(String[] args)
  {
    int N = 12;
    ArrayList max_sequence = maxSequence(N);
 
    // Printing the required sequence
    for (int i = 0; i < max_sequence.size(); i++) {
      System.out.print(max_sequence.get(i) + " ");
    }
  }
}
 
// This code is contributed by Rohit Pradhan


C#
// C# program to find Maximum length
// sequence containing distinct
// even integers with sum N
using System;
using System.Collections;
using System.Collections.Generic;
 
public class GFG{
 
  // Function to find maximum
  // length sequence of distinct
  // positive even integers
  // that adds up to N
  public static ArrayList maxSequence(int N)
  {
     
    // Initializing a arraylist
    // to store the sequence
    ArrayList v = new ArrayList();
 
    // Initializing sum and curr
    // that stores the
    // sum of sequence and current
    // even integer respectively
    int curr = 2, sum = 0;
 
    // If N is odd, just return
    // the empty arraylist
    // as the required sequence cannot
    // be deduced for an odd integer
    if (N % 2 == 1) {
      return v;
    }
 
    // While sum of sequence remains
    // less than or equal to N, we keep
    // inserting the value of curr in
    // arraylist v as well as adding into sum
    // also, increment curr by 2 at each
    // iteration to get next even number
    while (sum + curr <= N) {
      v.Add(curr);
      sum += curr;
      curr += 2;
    }
 
    int sz = v.Count;
 
    // We add the difference of N and
    // sum to the last digit of sequence
    int temp = sz - 1;
    v[temp]= (int)v[temp] + N - sum;
 
    // Returning the sequence
    return v;
  }
  static public void Main (){
    int N = 12;
    ArrayList max_sequence = maxSequence(N);
 
    // Printing the required sequence
    foreach (int i in max_sequence) {
      Console.Write(i + " ");
    }
  }
}
 
// This code is contributed by hrithikgarg03188.


Javascript



输出
2 4 6 

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