Rolling median function (Daily Coding Problem 377) Given an array of numbers arr and a window of size k, print out the
  median of each window of size k starting from the left and moving
  right by one position each time.
  
  For example, given the following array and k = 3:
  
  [-1, 5, 13, 8, 2, 3, 3, 1]
  
  Your function should print out the
  following:
  
  5 &lt;- median of [-1, 5, 13]
  8 &lt;- median of [5, 13, 8]
  8 &lt;- median of [13, 8, 2]
  3 &lt;- median of [8, 2, 3]
  3 &lt;- median of [2, 3, 3]
  3 &lt;- median of [3, 3, 1]
  
  Recall that the median of an even-sized list is the average of the two middle
  numbers.


Is there any improvement possible in this implementation?

    #include &lt;iostream&gt;
    #include &lt;vector&gt;
    #include &lt;array&gt;
    #include &lt;algorithm&gt;

    template &lt;typename T&gt;
    struct range
    {
        auto begin()const {return  a; }
        auto end()const {return b;}

        T a,b;
    };

    range&lt;const int*&gt;make_range(const int* begin, const int* end)
    {
        return {begin, end};
    }

    range&lt;const int *&gt; make_window(const std::vector&lt;int&gt;&amp; vec, size_t start, size_t end)
    {
        return make_range(vec.data() + start, vec.data() + end);
    }

    int median(int arr[], size_t N)
    {
        //sort the array
        std::sort(arr, arr + N);
        if(N % 2 == 0)
            return (arr[N/2 - 1] + arr[N/2])/2;
        return arr[N/2];
    }

     int main(){
        const auto window_length = 3;
        std::vector&lt;int&gt; input{-1, 5, 13, 8, 2, 3, 3, 1};
        const auto end_length = input.size() - window_length;
        std::array&lt;int, window_length&gt; arr{};


    for(size_t i(0); i &lt; end_length; i++)
    {
        int counter (0);
        const auto window = make_window(input, i, i + window_length);
        for(const auto&amp; value : window)
        {
            std::cout &lt;&lt; value &lt;&lt; ' ';
            arr[counter++] = value;
        }
        int result = median(arr.data(), arr.size());
        counter = 0;
        std::cout &lt;&lt; "\nmedian " &lt;&lt; result &lt;&lt; '\n';
        std::cout &lt;&lt; '\n';
    }

    return EXIT_SUCCESS; 
   }
This compiles cleanly and produces reasonable integer output (though not in the format required by the problem statement).

Note that you are required to include &lt;cstdlib&gt; before using EXIT_SUCCESS; failing to do so may be a portability bug.

All that copying and sorting gets expensive as the input and k both get larger.  We should probably create a stateful class that we can update with the new right-most value and the left-most to be removed.  I'd probably implement that with two sets - one holding values lower than the median and one holding higher values.  When we shift the window by one position, then we update the two sets and move an element if necessary to balance them (remember that because a set is sorted, it's easy to find the lowest and highest values using front() and back()).