Iterate the array twice while dynamically update the index of the max element inside current window.

```
public int[] maxSlidingWindow(int[] nums, int k) {
if(nums.length == 0 || k == 0) return new int[0];
int[] result = new int[nums.length-k+1];
int max = 0;
for(int i=0;i<nums.length;i++) {
if(i < k) {
if(nums[i] >= nums[max])
max = i;
}
else {
result[i-k] = nums[max];
if(nums[i] >= nums[max])
max = i;
else if(i-k == max) {
max ++;
for(int j=max;j<=i;j++) {
if(nums[j] >= nums[max])
max = j;
}
}
}
}
result[nums.length-k] = Math.max(nums[max], nums[nums.length-1]);
return result;
}
```