Java: 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 step in a given array of integers with duplicate numbers. Move the window to the top 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;
import java.util.LinkedList;
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;
}
Deque deque_num = new LinkedList();
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++) {
rst_arra.add(main_array[deque_num.peekFirst()]);
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);
}
rst_arra.add(main_array[deque_num.peekFirst()]);
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: Zenefits Google Amazon
Contribute your code and comments through Disqus.
Previous: Write a Java program to find the median of the number inside the window (size k) at each moving in a given array of intergers with duplicate numbers. Move the window from the start of the array.
Next: Write a Java program to delete a specified node in the middle of a singly linked list.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Java: Tips of the Day
DropElements
Removes elements in an array until the passed function returns true. Returns the remaining elements in the array.
Loop through the array, using Arrays.copyOfRange() to drop the first element of the array until the returned value from the function is true. Returns the remaining elements.
public static int[] dropElements(int[] elements, IntPredicate condition) { while (elements.length > 0 && !condition.test(elements[0])) { elements = Arrays.copyOfRange(elements, 1, elements.length); } return elements; }
Ref: https://bit.ly/37YWqzD
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook