Open In App

Generate longest Array containing distinct even integers with sum N

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:

Below is the implementation of the above approach:




// C++ program to find Maximum length
// sequence containing distinct
// even integers with sum N
 
#include <bits/stdc++.h>
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 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<Integer> maxSequence(int N)
  {
    // Initializing a arraylist
    // to store the sequence
    ArrayList<Integer> v = new ArrayList<Integer>();
 
    // 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<Integer> 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 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# 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.




<script>
       // JavaScript code for the above approach
 
       // Function to find maximum
       // length sequence of distinct
       // positive even integers
       // that adds up to N
       function maxSequence(N)
       {
        
           // Initializing a vector
           // to store the sequence
           let v = [];
 
           // Initializing sum and curr
           // that stores the
           // sum of sequence and current
           // even integer respectively
           let 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(curr);
               sum += curr;
               curr += 2;
           }
 
           let sz = v.length;
 
           // 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
 
       let N = 12;
       let max_sequence = maxSequence(N);
 
       // Printing the required sequence
       for (let i = 0; i < max_sequence.length; i++) {
           document.write(max_sequence[i] + " ");
       }
 
    // This code is contributed by Potta Lokesh
   </script>

 
 

Output
2 4 6 

 

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

 


Article Tags :