I'm trying to compete on HackerRank and my answer got accepted, but the times are not so good. I have a friend who sent the answer in C# too but somehow made it a lot faster. I'm wondering what can I do to improve it.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
class Solution {
static void Main(String[] args) {
int N;
StringBuilder st= new StringBuilder();
N = Convert.ToInt32(Console.ReadLine());
int[] x = new int[N];
List<double> a= new List<double>();
string[] s = new string[N];
for(int i=0; i<N ;i++){
string tmp = Console.ReadLine();
string[] split = tmp.Split(new Char[] {' ', '\t', '\n'});
s[i] = split[0].Trim();
x[i] = Convert.ToInt32(split[1].Trim());
bool r=true;
if(s[i]=="r"){
int index= a.BinarySearch(x[i]);
if(index>=0){
a.RemoveAt(a.LastIndexOf(x[i]));
}
else{
r=false;
}
}else{
var index = a.BinarySearch(x[i]);
if (index < 0) index = ~index;
a.Insert(index,x[i]);
}
if(!r || a.Count==0){
st.AppendLine("Wrong!");
}
else{
st.AppendLine(calcularModa(a).ToString());
}
}
Console.WriteLine(st.ToString());
}
static double calcularModa(List<double> a){
int i= a.Count/2;
if(a.Count % 2 !=0){
return a[i];
}else{
return ((a[i - 1] + a[i]))/2;
}
}
}