diff --git a/2294. Partition Array Such That Maximum Difference Is K b/2294. Partition Array Such That Maximum Difference Is K new file mode 100644 index 0000000..5b0dbfc --- /dev/null +++ b/2294. Partition Array Such That Maximum Difference Is K @@ -0,0 +1,16 @@ +class Solution { +public: + int partitionArray(vector& nums, int k) { + sort(nums.begin(), nums.end()); // 1. sort + int i = 0, n = nums.size(), groups = 0; + + while (i < n) { // 3. process one group + int limit = nums[i] + k; // smallest + k + i = upper_bound(nums.begin() + i, // first index > limit + nums.end(), + limit) - nums.begin(); + ++groups; // one group formed + } + return groups; // 4. answer + } +};