
Sponsored
Sponsored
Sort the array to ensure that the smallest numbers are adjacent, making it easier to form groups where the maximum difference is less than or equal to k. After sorting, iterate through the array and try to form groups of three. At each step, check if the difference between the first and third numbers in the potential group is less than or equal to k. If yes, form the group; otherwise, return an empty array as it's impossible to meet the requirement.
Time Complexity: O(n log n), due to the sorting step.
Space Complexity: O(1), as no additional space is used beyond the input and output storage.
This Python solution sorts the array with sort(), then traverses it to form groups of three numbers. It checks if the absolute difference between the minimum and maximum values in a group is ≤ k. If a valid group cannot be established, an empty list is returned.
This approach uses a greedy technique with two pointers to form groups of three elements. Sort the array first. Maintain two pointers, &&&i&&& and &&&j&&&, where &&&i&&& points to the start of a possible group and &&&j&&& iterates over the array to form a group when the criteria are met. When the triplet satisfies the requirement, move to the next possible group.
Time Complexity: O(n log n) for sorting, O(n) for the two pointers traversal, making it O(n log n).
Space Complexity: O(n) due to allocated space for the resulting groups.
1#include <stdio.h>
2#include <stdlib.h>
3
4int compare(const void *a, const void *b) {
5 return (*(int*)a - *(int*)b);
6}
7
8int** divideArrayIntoGroups(int* nums, int numsSize, int k, int* returnSize) {
9 if (numsSize % 3 != 0) {
10 *returnSize = 0;
11 return NULL;
12 }
13
14 qsort(nums, numsSize, sizeof(int), compare);
15 int** result = (int**)malloc(sizeof(int*) * (numsSize / 3));
16 int count = 0;
17 int i = 0, j = 0;
18
19 while (j < numsSize) {
20 if (j - i + 1 == 3) {
21 if (nums[j] - nums[i] <= k) {
22 int* group = (int*)malloc(sizeof(int) * 3);
23 group[0] = nums[i];
24 group[1] = nums[i+1];
25 group[2] = nums[j];
26 result[count++] = group;
27 i = j + 1;
28 j = i;
29 } else {
30 free(result);
31 *returnSize = 0;
32 return NULL;
33 }
34 } else {
35 j++;
36 }
37 }
38
39 *returnSize = count;
40 return result;
41}
42
43int main() {
44 int nums[] = {1,3,4,8,7,9,3,5,1};
45 int k = 2;
46 int numsSize = sizeof(nums) / sizeof(nums[0]);
47 int returnSize;
48 int** result = divideArrayIntoGroups(nums, numsSize, k, &returnSize);
49
50 if (returnSize == 0) {
51 printf("[]\n");
52 } else {
53 for (int i = 0; i < returnSize; i++) {
54 printf("[%d, %d, %d]\n", result[i][0], result[i][1], result[i][2]);
55 free(result[i]);
56 }
57 free(result);
58 }
59
60 return 0;
61}This C implementation uses a sorted array and a two-pointer technique. Pointers &&&i&&& and &&&j&&& are used to track possible triplets. The solution evaluates whether formed groups satisfy the maximum difference ≤ k. If a triplet exceeds the maximum difference, it raises an error and exits as formation is impractical.