LeetCode in Kotlin

3379. Transformed Array

Easy

You are given an integer array nums that represents a circular array. Your task is to create a new array result of the same size, following these rules:

For each index i (where 0 <= i < nums.length), perform the following independent actions:

Return the new array result.

Note: Since nums is circular, moving past the last element wraps around to the beginning, and moving before the first element wraps back to the end.

Example 1:

Input: nums = [3,-2,1,1]

Output: [1,1,1,3]

Explanation:

Example 2:

Input: nums = [-1,4,-1]

Output: [-1,-1,4]

Explanation:

Constraints:

Solution

import kotlin.math.abs

class Solution {
    fun constructTransformedArray(nums: IntArray): IntArray {
        val n = nums.size
        val res = IntArray(n)
        for (i in 0..<n) {
            if (nums[i] > 0) {
                res[i] = nums[(i + nums[i]) % n]
            } else if (nums[i] < 0) {
                val r: Int = abs(nums[i]) / n
                res[i] = nums[abs(i + nums[i] + r * n + n) % n]
            }
        }
        return res
    }
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy