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++
#include <bits/stdc++.h>
using namespace std;
vector< int > maxSequence( int N)
{
vector< int > v;
int curr = 2, sum = 0;
if (N % 2 == 1) {
return v;
}
while (sum + curr <= N) {
v.push_back(curr);
sum += curr;
curr += 2;
}
int sz = v.size();
v[sz - 1] += (N - sum);
return v;
}
int main()
{
int N = 12;
vector< int > max_sequence = maxSequence(N);
for ( int i = 0; i < max_sequence.size(); i++) {
cout << max_sequence[i] << " " ;
}
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
public static ArrayList<Integer> maxSequence( int N)
{
ArrayList<Integer> v = new ArrayList<Integer>();
int curr = 2 , sum = 0 ;
if (N % 2 == 1 ) {
return v;
}
while (sum + curr <= N) {
v.add(curr);
sum += curr;
curr += 2 ;
}
int sz = v.size();
v.set(sz - 1 ,v.get(sz- 1 )+(N-sum));
return v;
}
public static void main(String[] args)
{
int N = 12 ;
ArrayList<Integer> max_sequence = maxSequence(N);
for ( int i = 0 ; i < max_sequence.size(); i++) {
System.out.print(max_sequence.get(i) + " " );
}
}
}
|
Python3
def maxSequence(N):
v = []
curr, sum = 2 , 0
if (N % 2 = = 1 ):
return v
while ( sum + curr < = N):
v.append(curr)
sum + = curr
curr + = 2
sz = len (v)
v[sz - 1 ] + = (N - sum )
return v
if __name__ = = "__main__" :
N = 12
max_sequence = maxSequence(N)
for i in range ( 0 , len (max_sequence)):
print (max_sequence[i], end = " " )
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
public class GFG{
public static ArrayList maxSequence( int N)
{
ArrayList v = new ArrayList();
int curr = 2, sum = 0;
if (N % 2 == 1) {
return v;
}
while (sum + curr <= N) {
v.Add(curr);
sum += curr;
curr += 2;
}
int sz = v.Count;
int temp = sz - 1;
v[temp]= ( int )v[temp] + N - sum;
return v;
}
static public void Main (){
int N = 12;
ArrayList max_sequence = maxSequence(N);
foreach ( int i in max_sequence) {
Console.Write(i + " " );
}
}
}
|
Javascript
<script>
function maxSequence(N)
{
let v = [];
let curr = 2, sum = 0;
if (N % 2 == 1) {
return v;
}
while (sum + curr <= N) {
v.push(curr);
sum += curr;
curr += 2;
}
let sz = v.length;
v[sz - 1] += (N - sum);
return v;
}
let N = 12;
let max_sequence = maxSequence(N);
for (let i = 0; i < max_sequence.length; i++) {
document.write(max_sequence[i] + " " );
}
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...