aboutsummaryrefslogtreecommitdiff
path: root/divide_array_max_diff.c
diff options
context:
space:
mode:
authorjack <[email protected]>2024-06-14 13:13:15 -0600
committerjack <[email protected]>2024-06-14 13:13:15 -0600
commitd343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch)
tree96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /divide_array_max_diff.c
downloadleetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz
leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip
initial
Diffstat (limited to 'divide_array_max_diff.c')
-rw-r--r--divide_array_max_diff.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/divide_array_max_diff.c b/divide_array_max_diff.c
new file mode 100644
index 0000000..72e8648
--- /dev/null
+++ b/divide_array_max_diff.c
@@ -0,0 +1,35 @@
+// 2966. Divide Array Into Arrays With Max Difference
+#include "leetcode.h"
+
+/*
+ * given an integer array 'nums' of size 'n' and a positive integer 'k'. divide
+ * the array into one or more arrays of size 3 satisfying the following
+ * conditions. each element of 'nums' should be in exactly one array. the
+ * different between any two elements in one array is less than or equal to 'k'.
+ * return a 2d array containing all the arrays if it is impossible to satisfy
+ * the conditions, return an empty array. and if there are multiple answers,
+ * return any of them
+ */
+
+int cmp(const void *a, const void *b) { return (*(int *)a) - (*(int *)b); }
+
+int **divideArray(int *nums, int numsSize, int k, int *returnSize,
+ int **returnColumnSizes) {
+ int num_arr = numsSize / 3;
+ int **ans = (int **)calloc(num_arr, sizeof(int *));
+ *returnColumnSizes = (int *)calloc(num_arr, sizeof(int));
+ *returnSize = 0;
+ qsort(nums, numsSize, sizeof(int), cmp);
+ for (int i = 0; i < num_arr; i++) {
+ ans[i] = (int *)calloc(3, sizeof(int));
+ (*returnColumnSizes)[i] = 3;
+ for (int j = 0; j < 3; j++) {
+ int idx = i * 3 + j;
+ if (j && nums[idx] - nums[i * 3] > k)
+ return ans;
+ ans[i][j] = nums[idx];
+ }
+ }
+ *returnSize = num_arr;
+ return ans;
+}