1920. Build Array from Permutation ¶ Time: $O(n)$ Space: $O(1)$ C++JavaPython 1 2 3 4 5 6 7 8 9 10 11 12 13 14class Solution { public: vector<int> buildArray(vector<int>& nums) { const int n = nums.size(); for (int i = 0; i < n; ++i) nums[i] += n * (nums[nums[i]] % n); for (int i = 0; i < n; ++i) nums[i] /= n; return nums; } }; 1 2 3 4 5 6 7 8 9 10 11 12 13class Solution { public int[] buildArray(int[] nums) { final int n = nums.length; for (int i = 0; i < n; ++i) nums[i] += n * (nums[nums[i]] % n); for (int i = 0; i < n; ++i) nums[i] /= n; return nums; } } 1 2 3 4 5 6 7 8 9 10 11class Solution: def buildArray(self, nums: list[int]) -> list[int]: n = len(nums) for i, num in enumerate(nums): nums[i] += n * (nums[num] % n) for i in range(n): nums[i] //= n return nums