Grokking Data Structures & Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Solution: Divide Array Into Arrays With Max Difference
On this page

Problem Statement

You are given an array nums containing n integers and a positive integer k.

Divide the nums into arrays of size 3 such that it satisfies the below conditions:

  • Each element of nums should be in exactly one array.
  • The difference between any two elements of a single array should be less than or equal to k.

Return a 2D array of these subarrays. If no such division is possible, return an empty array.

Examples

Example 1:

  • Input: nums = [2, 6, 4, 9, 3, 7, 3, 4, 1], k = 3
  • Expected Output: [[1,2,3],[3,4,4],[6,7,9]]

.....

.....

.....

Like the course? Get enrolled and start learning!

On this page