﻿ Java programming exercises: Find the maximum number inside the number in the window - w3resource # Java Exercises: Find the maximum number inside the number in the window

## Java Basic: Exercise-174 with Solution

Write a Java program to find the maximum number inside the number in the window (size k) at each moving in a given array of integers with duplicate numbers. Move the window from the start of the array.

{|1, 2, 3|, 4, 5, 6, 7, 8, 8} -> Return maximum 3
{1, |2, 3, 4|, 5, 6, 7, 8, 8} -> Return maximum 4
{1, 2, |3, 4, 5|, 6, 7, 8, 8} -> Return maximum 5
{1, 2, 3, |4, 5, 6|, 7, 8, 8} -> Return maximum 6
{1, 2, 3, 4, |5, 6, 7|, 8, 8} -> Return maximum 7
{1, 2, 3, 4, 5, |6, 7, 8|, 8} -> Return maximum 8
{1, 2, 3, 4, 5, 6, |7, 8, 8|} -> Return maximum 8
Result array {3, 4, 5, 6, 7, 8, 8}

Sample Solution:

Java Code:

``````import java.util.*;
import java.util.Arrays;

public class Solution {
public static void main(String[] args) {
int[] main_array = {1, 2, 3, 4, 5, 6, 7, 8, 8};
int k = 3;
System.out.println("\nOriginal array: "+Arrays.toString(main_array));
System.out.println("\nValue of k: "+k);
System.out.println("\nResult: ");
ArrayList result = max_slide_window(main_array,k);
for (int i = 0; i < result.size(); i++) {
System.out.println(result.get(i));
}
}
public static ArrayList max_slide_window(int[] main_array, int k) {
ArrayList rst_arra = new ArrayList();
if (main_array == null || main_array.length == 0 || k < 0) {
return rst_arra;
}
for (int i = 0; i < k; i++) {
while (!deque_num.isEmpty() && main_array[deque_num.peekLast()] <= main_array[i]) {
deque_num.pollLast();
}
deque_num.offerLast(i);
}

for (int i = k; i < main_array.length; i++) {
if (!deque_num.isEmpty() && deque_num.peekFirst() <= i - k) {
deque_num.pollFirst();
}
while (!deque_num.isEmpty() && main_array[deque_num.peekLast()] <= main_array[i]) {
deque_num.pollLast();
}
deque_num.offerLast(i);
}
return rst_arra;
}
}
```
```

Sample Output:

```Original array: [1, 2, 3, 4, 5, 6, 7, 8, 8]

Value of k: 3

Result:
3
4
5
6
7
8
8
```

Pictorial Presentation: Flowchart: Java Code Editor:

Company:

What is the difficulty level of this exercise?

﻿

## Java: Tips of the Day

Array vs ArrayLists:

The main difference between these two is that an Array is of fixed size so once you have created an Array you cannot change it but the ArrayList is not of fixed size. You can create instances of ArrayLists without specifying its size. So if you create such instances of an ArrayList without specifying its size Java will create an instance of an ArrayList of default size.

Once an ArrayList is full it re-sizes itself. In fact, an ArrayList is internally supported by an array. So when an ArrayList is resized it will slow down its performance a bit as the contents of the old Array must be copied to a new Array.

At the same time, it's compulsory to specify the size of an Array directly or indirectly while creating it. And also Arrays can store both primitives and objects while ArrayLists only can store objects.

Ref: https://bit.ly/3o8L2KH