Help me in solving MXEVNSUB problem

My issue

I am getting correct output but after submission it shows wrong

My code

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
		for(int i=1;i<=t;i++)
		{
		  int n=sc.nextInt();
		 /* if(n>=2 && n<=10000)
		  {
		  if(n%4==0)
		  {
		    System.out.println(n);
		  }
		 else if( n==3 || ((n-3)%4==0))
		  {
		    System.out.println(n);
		  }
		  else if(n%2==0)
		  {
		    System.out.println(n-2);
		  }
		  else 
		  {
		    System.out.println(n-1);
		  }
		}*/
		if((n*(n+1)/2)%2==0)
		{
		  System.out.println(n);
		}
		else 
		{
		  if(n%2==0)
		  {
		    System.out.println(n-2);
		  }
		  else
		  {
		    System.out.println(n-1);
		  }
		}
}
}
}


Problem Link: CodeChef: Practical coding for everyone

@reddytanuj7
For n=2
your code will print 0 .
the answer would be 1.
take 2 as the subarray from [1,2];
will give me even sum of length 1.

Thanks for reply but I am not able to understand the line take 2 subarray from [1 2]

Like for n=2;
the elements would be 1,2 right .
So the total sum upto n will be 1+2 =3 .
which is odd so we cannot pick subarray of size 2 .
Now we have options u pick subarray of size of .from [1,2]
1 size subarray would be taking only 1 or taking only 2 .
so in case of taking only 2 , U will get your answer which is even sum .
Thus for this case answer would be 1 length.

1 Like

Would please write the code in Java I am unable to debug thank you

@reddytanuj7
Here u go

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
		for(int i=1;i<=t;i++)
		{
		  int n=sc.nextInt();
		  int sm=(n*(n+1))/2;
		  if(sm%2==0)
		  {
		    System.out.println(n);
		  }
		  else
		  {
		      System.out.println(n-1);
		  }
        }
    }
}