# MXEVNSUB - Editorial

Author: Daanish Mahajan
Tester: Anay Karnik
Editorialist: Mohan Abhyas

Cakewalk

None

# PROBLEM:

You are given an integer N. Consider the sequence containing the integers 1, 2, \ldots, N in increasing order (each exactly once). Find the maximum length of its contiguous subsequence with an even sum.

# EXPLANATION:

\sum_{k=1}^{k=n} k = n*(n+1)/2
Take whole sequence if above sum is even else leave out 1 to make sum odd

# TIME COMPLEXITY:

\mathcal{O}(1) per testcase.

# SOLUTIONS:

[details = “Editorial’s Solution”]

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define forn(i,e) for(ll i = 0; i < e; i++)

void solve()
{
ll n;
cin>>n;
ll sum = (n*(n+1))/2;
if(sum%2)
{
cout<<n-1<<endl;
}
else
{
cout<<n<<endl;
}
}

int main()
{
ll t=1;
cin >> t;
forn(i,t) {
solve();
}
return 0;
}


in case of 10 it will be , 2+3+4+5+6+7+8+9+10 = 54

1 Like

what if n=2 ?

then answer will be 1, {2}.

what’s wrong in this code??

#include
using namespace std;

int main() {
int t;
cin>>t;
while(t!=0)
{
int n;
cin>>n;
int count=n;
int sum;
for(int i=1;i<=n;i++)
{
sum+=i;
}
if(sum%2!=0)
{
sum=sum-(n);
count–;
if(sum%2!=0)
{
count–;
cout<<count<<endl;
}
else
{
cout<<count<<endl;
}
}
else
{
cout<<count<<endl;
}
t–;
}
return 0;
}

Can someone help in this?

This solution would be the ideal solution in my opinion

t=int(input())
while(t):
n=int(input())
if(((n*(n+1))/2)%2==0):
print(n)
else:
print(n-1)
t-=1


You should check the sum of entire series. In that way if sum is odd just one can be removed to make it largest even sum subsequence
What you’re doing fails for numbers like 10. (Ans should be 9 but your code gives 8)!
Hope this helps even a little bit!

1 Like

At 10 code provides 9 as output😐