commit 4e20b6830acfc87db121204b472dc0da62f4ef28
parent 9bb647b8c4088371b3152a7df519281f3ff18d28
Author: Jack Sangdahl <jack@pngu.org>
Date: Wed, 4 Feb 2026 20:35:23 -0700
3379. transformed array
Diffstat:
2 files changed, 77 insertions(+), 0 deletions(-)
diff --git a/transformed_array.c b/transformed_array.c
@@ -0,0 +1,46 @@
+// 3379. Transformed Array
+#include "leetcode.h"
+
+/*
+ * you are given an integer array 'nms' that represents a circular array. your
+ * task is to create a new array 'res' of the same size, following these rules.
+ * for each index 'i', perform the following independent actions. if 'nums[i] >
+ * 0' start at index 'i' and move 'nums[i]' steps to the right in the circular
+ * array. set 'ans[i]' to the value of the index where you land. if 'nums[i] <
+ * 0', start at index 'i' and move 'abs(nums[i])' steps to the left in the
+ * circular array. set 'ans[i]' to the value of the index where you land. return
+ * new array 'ans'.
+ */
+
+int *constructTransformedArray(int *nums, int numsSize, int *returnSize) {
+ const int shift = 1 << 7, mask = (1 << 8) - 1;
+ *returnSize = numsSize;
+ for (int i = 0; i < numsSize; i++)
+ nums[i] += shift;
+ for (int i = 0; i < numsSize; i++) {
+ int curr = nums[i] - shift;
+ int idx = ((i + curr) % numsSize + numsSize) % numsSize;
+ int val = nums[idx] & mask;
+ nums[i] |= val << 8;
+ }
+ for (int i = 0; i < numsSize; i++)
+ nums[i] = (nums[i] >> 8) - shift;
+ return nums;
+}
+
+int main() {
+ int n1[] = {3, -2, 1, 1}, r1[] = {1, 1, 1, 3}, rs1;
+ int n2[] = {-1, 4, -1}, r2[] = {-1, -1, 4}, rs2;
+ int *ctfa1 = constructTransformedArray(n1, ARRAY_SIZE(n1), &rs1);
+ int *ctfa2 = constructTransformedArray(n2, ARRAY_SIZE(n2), &rs2);
+ for (int i = 0; i < rs1; i++) {
+ printf("%d ", ctfa1[i]);
+ assert(r1[i] == ctfa1[i]);
+ }
+ printf("\n");
+ for (int i = 0; i < rs2; i++) {
+ printf("%d ", ctfa2[i]);
+ assert(r2[i] == ctfa2[i]);
+ }
+ printf("\n");
+}
diff --git a/transformed_array.py b/transformed_array.py
@@ -0,0 +1,31 @@
+# 3379. Transformed Array
+
+"""
+you are given an integer array 'nms' that represents a circular array. your
+task is to create a new array 'res' of the same size, following these rules.
+for each index 'i', perform the following independent actions. if 'nums[i] >
+0' start at index 'i' and move 'nums[i]' steps to the right in the circular
+array. set 'ans[i]' to the value of the index where you land. if 'nums[i] <
+0', start at index 'i' and move 'abs(nums[i])' steps to the left in the
+circular array. set 'ans[i]' to the value of the index where you land. return
+new array 'ans'.
+"""
+
+
+class Solution(object):
+ def constructTransformedArray(self, nums):
+ """
+ :type nums: List[int]
+ :rtype: List[int]
+ """
+ n = len(nums)
+ ans = [0] * n
+ for i, j in enumerate(nums):
+ ans[i] = nums[(i + j) % n]
+ return ans
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.constructTransformedArray(nums=[3, -2, 1, 1]))
+ print(obj.constructTransformedArray(nums=[-1, 4, -1]))