LeetCode 561. Array Partition || Java Solution Walkthrough

Опубликовано: 06 Апрель 2026
на канале: Unknown Koder
303
3

LeetCode is a great platform for people who want general coding practice, whether that be for interviews or just wanting to practice their problem solving. I will be taking you through all the steps of finding the optimal solution to each problem on the site following a logical ordering setout by LeetCode themselves.

In this video, we are going over the problem 561. Array Partition and the goal is to loop through the array of numbers and come up with a new list of ordered numbers, such that all the mimimum values in those sets add up to the largest possible number from the list.

To solve this problem is a little bit more complicated and confusion to get a grasp of. As the length of the nums array increases, the possible sets of numbers also increases expontentially, so we have to be smart about how we are checking these pairs, otherwise we could run out of time. To solve this problem we are going to use a couple of interesting tricks. Firstly, we need some time effiecent way to sort the array from smallest to largest, as well as keep track of how many of each number we see, we can do this by setting up a new array that is twice the size of the max number. Once the array is sorted, the solution follows a pattern of choosing every other element to be added to the total. We can use some tricks with bitwise shifting, bitwise and, and bitwise xor to find the total and when to skip an element. Lets take a look at the pseudo code and an example walkthrough before hopping onto leetcode.

~~~ Stay Up To Date With My Social Media ~~~

Twitter:   / unknownkoder  
Twitch:   / unknownkoderyt  
Be sure to subscribe and turn on the bell notifications on youtube to not miss another episode of Lets Build Twitter: shorturl.at/gnxZ8

#unknownkoder #leetcode #algorithms