2966: Divide-Array-Into-Arrays-With-Max-Difference
Medium
table of contents
It is just another greedy problem, where you
sort the array, and then subdivide into groups of
3
, checking if all the groups you form are valid or
not.
This is very similar to tomorrow’s problem.
code
class Solution {
public:
<vector<int>> divideArray(vector<int>& nums, int k) {
vector(nums.begin(), nums.end());
sort<vector<int>> ans;
vectorfor (int i = 0; i < nums.size(); i += 3) {
if (nums[i] + k >= nums[i+2]) {
.push_back({nums[i], nums[i+1], nums[i+2]});
ans} else {
return {};
}
}
return ans;
}
};