Introduction
The “Max Chunks to Make Sorted II” problem on LeetCode is a fascinating challenge that tests your understanding of sorting algorithms and array manipulation. In this blog, we’ll dive deep into the problem, understand the requirements, and explore an efficient solution in Java.
Problem Description
LeetCode Problem: Max Chunks to Make Sorted II
You are given an array arr
of integers (not necessarily distinct). We split the array into some number of “chunks” (partitions), and individually sort each chunk. After concatenating them, the result should be sorted.
Here is another image of a programmer working on the “Max Chunks to Make Sorted II” problem in Java, with a modern and focused workspace. Let me know if this meets your needs or if any adjustments are required!

What is the most number of chunks we could have made?
Example:
Input: arr = [2,1,3,4,4]
Output: 4
Explanation:
We can split into two chunks, such as [2, 1], [3, 4, 4].
Note:
arr
will have length in range [1, 2000].arr[i]
will be an integer in range [0, 10^8].
Keywords
- Java
- Array Manipulation
- Sorting Algorithms
- LeetCode Solutions
- Coding Challenge
- Algorithm Problems
- Developer Guide
- Java Programming
Solution Approach
The core idea to solve this problem is to use two arrays, maxLeft
and minRight
. The maxLeft[i]
stores the maximum value from the start of the array up to index i
. The minRight[i]
stores the minimum value from index i
to the end of the array. Using these two arrays, we can determine where we can make valid chunks.

Here’s a step-by-step solution in Java:
import java.util.Arrays;
public class MaxChunksToMakeSortedII {
public int maxChunksToSorted(int[] arr) {
int n = arr.length;
int[] maxLeft = new int[n];
int[] minRight = new int[n];
// Fill maxLeft array
maxLeft[0] = arr[0];
for (int i = 1; i < n; i++) {
maxLeft[i] = Math.max(maxLeft[i - 1], arr[i]);
}
// Fill minRight array
minRight[n - 1] = arr[n - 1];
for (int i = n - 2; i >= 0; i--) {
minRight[i] = Math.min(minRight[i + 1], arr[i]);
}
// Count the chunks
int chunks = 0;
for (int i = 0; i < n - 1; i++) {
if (maxLeft[i] <= minRight[i + 1]) {
chunks++;
}
}
// Since we can always make one chunk, add 1 to chunks
return chunks + 1;
}
public static void main(String[] args) {
MaxChunksToMakeSortedII solution = new MaxChunksToMakeSortedII();
int[] arr = {2, 1, 3, 4, 4};
System.out.println("Max chunks to make sorted: " + solution.maxChunksToSorted(arr));
}
}
Explanation
- maxLeft Array: It keeps track of the maximum value from the start up to the current index. This helps in knowing the largest element in the left part of the array.
- minRight Array: It keeps track of the minimum value from the current index to the end of the array. This helps in knowing the smallest element in the right part of the array.
- Counting Chunks: By comparing
maxLeft[i]
andminRight[i+1]
, we determine where we can split the array into valid chunks.
Conclusion
The “Max Chunks to Make Sorted II” problem is a great way to test your understanding of array manipulation and sorting algorithms in Java. By using auxiliary arrays to keep track of maximum and minimum values, we can efficiently solve this problem. Happy coding!
Read More …
Introduction to SQL Programming: A Beginner’s Guide – https://kamleshsingad.com/introduction-to-sql-programming-a-beginners-guide/
Top 10 SQL Programming Tips for Beginners – https://kamleshsingad.com/top-10-sql-programming-tips-for-beginners/
Understanding SQL Joins: A Comprehensive Guide – https://kamleshsingad.com/understanding-sql-joins-a-comprehensive-guide/