Given a list of integers and a window size k, find the maximum value in each sliding window of size k as it moves from left to right across the list. Each window contains k consecutive elements, and the window slides one position at a time. Return a list of maximum values, one for each window position. Return an empty list for null or empty input, or when k is less than 1.

If k is greater than the list size, treat the entire list as a single window and return the maximum of all elements.

Method signature: public static List<Integer> slidingWindowMax(List<Integer> nums, Integer k)

Examples:

Input: nums = [1, 3, -1, -3, 5, 3, 6, 7], k = 3 Output: [3, 3, 5, 5, 6, 7] Explanation: Window [1,3,-1]=3, [3,-1,-3]=3, [-1,-3,5]=5, [-3,5,3]=5, [5,3,6]=6, [3,6,7]=7 Input: nums = [1, 2, 3], k = 1 Output: [1, 2, 3] Explanation: Each element is its own window Input: nums = [4, 2, 7], k = 3 Output: [7] Explanation: Only one window spans the entire list Input: nums = [5, 5, 5, 5], k = 2 Output: [5, 5, 5] Explanation: All values are the same, each window maximum is 5
Apex Code Editor

Welcome to Lightning Challenge!

Create an Account

Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.

Create account

How It Works

  • • Write your solution in the code editor
  • • Connect your Salesforce org to test
  • • Submit to check if your solution passes
  • • Use hints if you get stuck

Contest Alert

🏆 #CodeEveryDay May 2026

Contest runs May 1 - 31. Complete challenges to climb the leaderboard!

Only the 31 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In