0% found this document useful (0 votes)
7 views3 pages

L22Sliding Window Concept in Array Programming

Uploaded by

dp148026
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

L22Sliding Window Concept in Array Programming

Uploaded by

dp148026
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

L22:Sliding Window Concept in

Array Programming
Problem Explanation
Given an array of integers and a window size \( k \), the goal is to find the
maximum value within each window as it slides from the start to the end of the
array. For example, if the array is [3, 5, 4, 8, 6, 7, 2, 9] and the window size is 3,
the task is to find the maximum values for each subset of 3 consecutive
elements.

Steps to Solve the Problem


1. Understanding the Inputs and Outputs:

Input: An array of integers and a window size \( k \).

Output: An array of maximum values from each sliding window.

2. Initialize Variables:

An array to store the input values.

Variables to hold the size of the array and the window size \( k \).

A variable to track the maximum value in the current window.

3. Sliding Window Mechanism:

Use nested loops to iterate through the array.

The outer loop defines the starting point of the window.

The inner loop iterates through the elements within the window to find
the maximum value.

Update the maximum value as needed while iterating through the


window.

Example
Consider the array [3, 5, 4, 8, 6, 7, 2, 9] with a window size \( k = 3 \):

1. The first window is [3, 5, 4]. The maximum value is 5.

2. The second window is [5, 4, 8]. The maximum value is 8.

L22:Sliding Window Concept in Array Programming 1


3. The third window is [4, 8, 6]. The maximum value is 8.

4. The fourth window is [8, 6, 7]. The maximum value is 8.

5. The fifth window is [6, 7, 2]. The maximum value is 7.

6. The sixth window is [7, 2, 9]. The maximum value is 9.

The output array of maximum values is [5, 8, 8, 8, 7, 9].

Implementation in Java

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Input size of the array


System.out.println("Enter the size of the array:");
int n = scanner.nextInt();

// Create the array and take input values


int[] array = new int[n];
System.out.println("Enter the elements of the arra
y:");
for (int i = 0; i < n; i++) {
array[i] = scanner.nextInt();
}

// Input the window size


System.out.println("Enter the window size:");
int k = scanner.nextInt();

// Implement the sliding window algorithm


for (int i = 0; i <= n - k; i++) {
int max = array[i];
for (int j = i; j < i + k; j++) {
if (array[j] > max) {
max = array[j];
}

L22:Sliding Window Concept in Array Programming 2


}
System.out.print(max + " ");
}
}
}

Detailed Explanation of Code


1. Input Handling:

The size of the array \( n \) and the elements of the array are taken as
input.

The window size \( k \) is also taken as input.

2. Outer Loop:

Iterates from the start of the array to \( n - k \). This ensures the window
does not exceed the array boundaries.

3. Inner Loop:

For each position of the outer loop, the inner loop iterates from the
current index \( i \) to \( i + k - 1 \).

It compares each element within the window to find the maximum


value.

4. Output:

The maximum value of each window is printed.

Debugging and Optimization Tips


1. Initialization:

Ensure the max variable is initialized at the start of each new window.

2. Edge Cases:

Consider edge cases such as when \( k \) is larger than the array size.

3. Efficiency:

The given approach has a time complexity of \( O(n \times k) \). For
larger arrays and window sizes, consider using more efficient methods
like Deque to achieve \( O(n) \) complexity.

L22:Sliding Window Concept in Array Programming 3

You might also like