LeetCode: Sliding Window Median C# https://leetcode.com/problems/sliding-window-median/


  Median is the middle value in an ordered integer list. If the size of
  the list is even, there is no middle value. So the median is the mean
  of the two middle value.
  
  Examples: [2,3,4] , the median is 3
  
  [2,3], the median is (2 + 3) / 2 = 2.5
  
  Given an array nums, there is a sliding window of size k which is
  moving from the very left of the array to the very right. You can only
  see the k numbers in the window. Each time the sliding window moves
  right by one position. Your job is to output the median array for each
  window in the original array.
  
  For example, Given nums = [1,3,-1,-3,5,3,6,7], and k = 3.





  Therefore, return the median sliding window as [1,-1,-1,3,5,6].
  
  Note: You may assume k is always valid, ie: k is always smaller than
  input array's size for non-empty array. Answers within 10^-5 of the
  actual value will be accepted as correct.


Please review for performance. also I am not sure about GetID function is there a smarter way to get the same result of unique number and sorting for median? 

using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TreeQuestions
{
    /// &lt;summary&gt;
    /// https://leetcode.com/problems/sliding-window-median/
    /// &lt;/summary&gt;
    [TestClass]
    public class SlidingWindowMedianTest
    {
        [TestMethod]
        public void ExampleTest()
        {
            int[] nums = { 1, 3, -1, -3, 5, 3, 6, 7 };
            int k = 3;
            double[] expected = { 1, -1, -1, 3, 5, 6 };
            double[] res = SlidingWindowMedian.MedianSlidingWindow(nums, k);
            CollectionAssert.AreEqual(expected, res);
        }

        [TestMethod]
        public void ExampleTestFailed()
        {
            int[] nums = { 1, 4, 2, 3 };
            int k = 4;
            double[] expected = { 2.5 };
            double[] res = SlidingWindowMedian.MedianSlidingWindow(nums, k);
            CollectionAssert.AreEqual(expected, res);
        }

        [TestMethod]
        public void ExampleTestOverFlow()
        {
            int[] nums = { 2147483647, 2147483647 };
            int k = 2;
            double[] expected = { 2147483647.0 };
            double[] res = SlidingWindowMedian.MedianSlidingWindow(nums, k);
            CollectionAssert.AreEqual(expected, res);
        }

        [TestMethod]
        public void ExampleFailed2()
        {
            int[] nums = { 5, 2, 2, 7, 3, 7, 9, 0, 2, 3 };
            int k = 9;
            double[] expected = { 3.0, 3.0 };
            double[] res = SlidingWindowMedian.MedianSlidingWindow(nums, k);
            CollectionAssert.AreEqual(expected, res);
        }
    }



    public class SlidingWindowMedian
    {
        public static double[] MedianSlidingWindow(int[] nums, int k)
        {
            var res = new List&lt;double&gt;();
            var med = new SortedList&lt;long, int&gt;();
            for (int i = 0; i &lt; nums.Length; i++)
            {
                med.Add(GetId(i, nums), nums[i]);
                if (med.Count &gt; k)
                {
                    med.Remove(GetId(i - k, nums));
                }

                if (med.Count == k)
                {
                    if (k % 2 == 0)
                    {
                        res.Add( ((long)med[med.Keys[k / 2 - 1]]  + med[med.Keys[k / 2]])/2.0 );
                    }
                    else
                    {
                        res.Add(med[med.Keys[k / 2]]);
                    }
                }
            }
            return res.ToArray();

        }

        private static long GetId(int i, int[] nums)
        {
            return Convert.ToInt64(nums[i]) * nums.Length + i;
        }
    }

}
Your ID is a long created from 2 ints, so you can fit both ints inside this one long side by side:

long key = (long)nums[i] &lt;&lt; 32 | i;




You have a lot of unnecessary if statement evaluations. You could get rid of them by adding the first k elements from the array into the list in a separate loop, and then starting a second loop from i = k that can safely do med.Remove and med.Add without ifs.

k never changes, so instead of evaluating k % 2 == 0 and k / 2 and k / 2 - 1 in every iteration, you can store the final value in a variable before the loop begins. Then in the loop you only need to call

res.Add(med[med.Keys[medianIndex]]);


or

res.Add( ((long)med[med.Keys[medianIndex]] + med[med.Keys[medianIndex2]])/2.0 );




Making res a list and in the end converting it into an array slows your code down a lot. You can make it an array (of size nums.Length - k + 1). Lists come with the overhead of their extra functionality, so you waste time not only on the conversion to an array but also on every time you add a number to the list.