Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Sliding Window Median problem solution

YASH PAL, 31 July 2024

In this Leetcode Sliding Window Median problem solution Given an integer n, return the largest palindromic integer that can be represented as the product of two n-digits integers. Since the answer can be very large, return it modulo 1337.

Leetcode Sliding Window Median problem solution

Problem solution in Python.

def medianSlidingWindow(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: List[float]
        """
        import bisect
        left = []
        right = []
        result = []

        for i, n in enumerate(nums):
            if i + 1 > k:
                pop_idx = i - k
                if nums[pop_idx] <= left[-1]:
                    pos = bisect.bisect_left(left, nums[pop_idx])
                    left.pop(pos)
                else:
                    pos = bisect.bisect_left(right, nums[pop_idx])
                    right.pop(pos)

            if not left or n > left[-1]:
                bisect.insort(right, n)
            else:
                bisect.insort(left, n)

            while len(left) > len(right) + 1:
                x = left.pop(-1)
                bisect.insort(right, x)

            while len(right) > len(left):
                x = right.pop(0)
                bisect.insort(left, x)

            if i + 1 >= k:
                if len(left) > len(right):
                    result.append(left[-1])
                else:
                    result.append((left[-1] + right[0]) / 2.0)
        return result

Problem solution in Java.

class Solution {
    public double[] medianSlidingWindow(int[] nums, int k) {
        int n = nums.length;
        double[] res = new double[n - k + 1];
        int i = 0; //index of res

        int l = 0, r = k - 1;
        while (r < n) {
            int[] tmp = Arrays.copyOfRange(nums, l, r + 1);
            Arrays.sort(tmp);
            if (k % 2 == 1) {
                res[i++] = tmp[k / 2] * 1.0;
            } else {
                res[i++] = (tmp[k / 2 - 1] * 1.0 + tmp[k / 2] * 1.0) / 2;
            }
            l++; r++;
        }
        return res;
    }
}

Problem solution in C++.

class Solution {
public:
    vector<double> medianSlidingWindow(vector<int>& nums, int k) {
        multiset<int> ms;
        for (int i = 0; i < k - 1; ++i)
            ms.insert(nums[i]);
        
        vector<double> res;
        
        for (int i = k - 1; i < nums.size(); ++i) {
            ms.insert(nums[i]);
            auto it = next(ms.begin(), (k - 1) / 2);
            res.push_back(k % 2 ? *it : ((static_cast<double>(*it) + *next(it)) / 2));
            ms.erase(ms.lower_bound(nums[i - k + 1]));
        }
        
        return res;
    }
};
coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes