Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Find median in a stream
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { /* Find median in a stream - List should be sorted. - if list size is base mdedian is (n+1)/2 th number - if list size is even , median is average of n/2 number and (n+2)/2 number */ static int sum = 0; static int median = 0; //static Hashse hs = new Hashset(); public static void Main(string[] args) { //Your code goes here Console.WriteLine("Hello, world!"); SortedList lst = new SortedList(); addNum(5, lst); addNum(15, lst); addNum(1, lst); addNum(3, lst); addNum(6, lst); addNum(8, lst); } public static void addNum(int n, SortedList lst) { //Console.WriteLine(" The arr size is " + lst.Count()); lst.Add(n,n); int count = lst.Count; if( count % 2 == 0 ){ int n1 = (int) lst.GetByIndex( (count/2) -1 ) ; int n2 = (int) lst.GetByIndex((count + 2 )/2 - 1); Console.WriteLine(" The median is " + (n1 + n2 ) / 2 ); } else { int n1 =0; if(count == 1) n1= (int) lst.GetByIndex(0); else n1= (int) lst.GetByIndex( (count + 1)/2 - 1) ; Console.WriteLine(" The median is " + n1 ); } } } }
run
|
edit
|
history
|
help
0
Finding occurances of a value in the elements of a list
Count vowels in a string
string formater placehoder
Interesting dictionary initialization syntax
ReRoute Emails
megha
lasses, properties, methods, ling begining
DateTime change clock
Main2
Lesson 1.2 Part I