 Open in App
Not now

# Generate longest Array containing distinct even integers with sum N

• Last Updated : 26 Apr, 2022

Given an integer N. Find the maximum length sequence containing distinct even integers with sum N. In case, such a sequence does not exist, return an empty sequence.

Examples:

Input: N = 12
Output : {2, 6, 4}
Explanation: 2, 6 and 4 are even and 2+6+4=12. Note that {4, 8} sequence also contains even numbers whose sum is 12 . But we need the sequence of maximum length.

Input: N = 25
Output: {}
Explanation: No sequence of distinct positive even integers is possible so that they add up to 25.

Approach: This problem can be solved using the greedy approach.

Start inserting even numbers from 2 into a list with a sum less than or equal to N. If a subsequence of even numbers with a sum equal to N is not possible simply add the remaining difference after all iterations to the last number in the list.

The following approach can be followed to solve the problem:

• If N is odd return the empty vector, as an odd number cannot be represented as the sum of even numbers.
• Initialize a variable (say sum) by 0, to store the sum of elements in a sequence, and a variable (say curr), to store the current even number.
• Until the value of sum is less than N, iteratively keep pushing values of curr in the answer and keep adding these values to sum. Also, keep incrementing the value of curr by 2 at each iteration.
• In end, add the value of remainder (N-sum) to the last element of the sequence.

Below is the implementation of the above approach:

## 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<``int``> maxSequence(``int` `N)` `{` `    ``// Initializing a vector` `    ``// to store the sequence` `    ``vector<``int``> 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<``int``> 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`

## Python3

 `# Python3 program to find Maximum length` `# sequence containing distinct` `# even integers with sum N`   `# Function to find maximum` `# length sequence of distinct` `# positive even integers` `# that adds up to N` `def` `maxSequence(N):`   `    ``# Initializing a vector` `    ``# to store the sequence` `    ``v ``=` `[]`   `    ``# Initializing sum and curr` `    ``# that stores the` `    ``# sum of sequence and current` `    ``# even integer respectively` `    ``curr, ``sum` `=` `2``, ``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.append(curr)` `        ``sum` `+``=` `curr` `        ``curr ``+``=` `2`   `    ``sz ``=` `len``(v)`   `    ``# 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` `if` `__name__ ``=``=` `"__main__"``:`   `    ``N ``=` `12` `    ``max_sequence ``=` `maxSequence(N)`   `    ``# Printing the required sequence` `    ``for` `i ``in` `range``(``0``, ``len``(max_sequence)):` `        ``print``(max_sequence[i], end``=``" "``)`   `    ``# This code is contributed by rakeshsahni`

## 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

 ``

Output

`2 4 6 `

Time Complexity: O(N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up
Related Articles