Getting RE(SIGSEGV)
whats wrong with the below code?
https://www.codechef.com/viewsolution/48240530
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.
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