I was trying to solve the Median challenge at Interviewstreet.com.
I wasn't able to pass most of the test cases. But according to my understanding my code should work fine.
Here is the question: https://www.interviewstreet.com/challenges/dashboard/#problem/4fcf919f11817
Here is my code:
/* Sample program illustrating input and output */
import java.util.*;
class Solution{
public static void main( String args[] ){
// helpers for input/output
Scanner in = new Scanner(System.in);
int N;
N = in.nextInt();
String s[] = new String[N];
int x[] = new int[N];
ArrayList<Integer> items=new ArrayList<Integer>();
float result[]=new float[N];
//int itemindex=-1;
for(int i=0; i<N; i++){
s[i] = in.next();
x[i] = in.nextInt();
switch(s[i].charAt(0))
{
case 'r':
if(items.size()<=1)
{
result[i]=-1;
}
else
{
items.remove((Integer)x[i]);
int itemindex=items.size();
Collections.sort(items);
if((itemindex)%2==0)
{
result[i]=(float) ((items.get((itemindex/2))+items.get(((itemindex)/2)-1))/2.0);
}
else
{
result[i]=items.get((itemindex-1)/2);
}
}
break;
case 'a':
items.add(x[i]);
int itemindex=items.size();
Collections.sort(items);
if((itemindex)%2==0)
{
result[i]=(float)((items.get((itemindex/2))+items.get(((itemindex)/2)-1))/2.0);
}
else
{
result[i]=items.get((itemindex-1)/2);
}
break;
}
}
for(int j=0;j<N;j++)
{
if(result[j]==-1)
{
System.out.println("Wrong!");
}
else
{
if((result[j]*10)%10==0)
{
System.out.println((int)(result[j]));
}
else
{
System.out.println(result[j]);
}
}
}
}
}
I would highly appreciate if someone could help me find out the test cases which will fail for my code.
Thanks.