aboutsummaryrefslogtreecommitdiff
path: root/shuffle_array.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 /shuffle_array.c
downloadleetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz
leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip
initial
Diffstat (limited to 'shuffle_array.c')
-rw-r--r--shuffle_array.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/shuffle_array.c b/shuffle_array.c
new file mode 100644
index 0000000..0192604
--- /dev/null
+++ b/shuffle_array.c
@@ -0,0 +1,31 @@
+// 1470. Shuffle the Array
+#include <stdio.h>
+#include <stdlib.h>
+
+/*
+ * given array 'nums' consisting of '2n' elements in the form
+ * [x1, x2, ..., xn, y1, y2, ..., yn]
+ * return the array in the form [x1, y1, x2, y2, ..., xn, yn]
+ */
+
+int *shuffle(int *nums, int numsSize, int n, int *returnSize) {
+ int i = 0, j = n, k = 0;
+ int *ans = (int *)malloc(numsSize * sizeof(int));
+ while (i < n && j < numsSize) {
+ ans[k++] = nums[i];
+ ans[k++] = nums[j];
+ i++;
+ j++;
+ }
+ *returnSize = numsSize;
+ return ans;
+}
+
+int main() {
+ int nums1[] = {2, 5, 1, 3, 4, 7}, rs1[] = {6}, ns1 = 6, n1 = 3;
+ int nums2[] = {1, 2, 3, 4, 4, 3, 2, 1}, rs2[] = {8}, ns2 = 8, n2 = 4;
+ int nums3[] = {1, 1, 2, 2}, rs3[] = {4}, ns3 = 4, n3 = 2;
+ printf("%d\n", *shuffle(nums1, ns1, n1, rs1));
+ printf("%d\n", *shuffle(nums2, ns2, n2, rs2));
+ printf("%d\n", *shuffle(nums3, ns3, n3, rs3));
+}