UNONE - Editorial

Getting RE(SIGSEGV)
whats wrong with the below code?
https://www.codechef.com/viewsolution/48240530

I love the explanation. Thanks @cherry0697 .

why i got TLE for same logic coded in Java

/* 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
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t–>0){
int n=sc.nextInt();
ArrayList odd=new ArrayList<>();
ArrayList even=new ArrayList<>();
for(int i=0;i<n;i++){
int x=sc.nextInt();
if(x%2==0)
even.add(x);
else
odd.add(x);
}
for(Integer m:even)
System.out.print(m+" “);
for(Integer m:odd)
System.out.print(m+” ");
System.out.println();

	}
}

}

Test case: 5 4 2
Answer: 2 4 5 or 4 2 5
Your Output: 2 5 4 ( Here you don’t use 4 optimally to reduce ugliness )

Suppose there’s no odd number, then odd_ct.size() will be 0. Whenever you use size function, it always return you in Unsigned long long type (which accepts values >= 0). Now you have used this code in your function odd_ct.size()-1. So now odd_ct.size() is 0 and you’re subtracting 1 from it, causing underflow, therefore causing RE.
Try submitting it by casting to int whenever using size function.
Thank you. :slight_smile:

Excellent Editorial !! .I really loved the proof part @cherry0697

no my outputwill be : 4 2 5
…my output always ends with odd no
once look at he code ,it will give you a clear idea

Try this out

1
3
2 4 6

You get empty, i think.

ok got it . Thanks a lot

By editorial’s solution what will be the output of :
2 2 2 7 7 7 ???
= 10 10 10 111 111 111 ugliness = 8

Yes…it’ll be 8