3379. Transformed Array
MediumView on LeetCode
Time: O(n)
Space: O(n)
Advertisement
Approach
For each element follow the circular pointer chain; use modular arithmetic.
Key Techniques
Array
Array problems involve manipulating elements stored in a contiguous block of memory. Key techniques include two-pointer traversal, prefix sums, sliding windows, and in-place partitioning. In C#, arrays are zero-indexed and fixed in size — use List<T> when you need dynamic resizing.
Simulation
Simulation problems require implementing the described process step by step. Focus on correctly handling edge cases and state transitions. Common in geometry, game problems, and string manipulation. Optimize only if the naive simulation exceeds the time limit.
3379.cs
C#
// Approach: For each element follow the circular pointer chain; use modular arithmetic.
// Time: O(n) Space: O(n)
public class Solution
{
public int[] ConstructTransformedArray(int[] nums)
{
int n = nums.Length;
int[] ans = new int[n];
for (int i = 0; i < n; ++i)
ans[i] = nums[(i + nums[i] % n + n) % n];
return ans;
}
}Advertisement
Was this solution helpful?