Skip to content

3379. Transformed Array 👍

  • Time: $O(n)$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
 public:
  vector<int> constructTransformedArray(vector<int>& nums) {
    const int n = nums.size();
    vector<int> ans;

    for (int i = 0; i < n; ++i)
      ans.push_back(nums[(i + nums[i] % n + n) % n]);

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
  public int[] constructTransformedArray(int[] nums) {
    final 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;
  }
}
1
2
3
4
5
class Solution:
  def constructTransformedArray(self, nums: list[int]) -> list[int]:
    n = len(nums)
    return [nums[(i + num % n + n) % n]
            for i, num in enumerate(nums)]