LeetCode Entry
3379. Transformed Array
Shuffled list i+n[i]
3379. Transformed Array easy blog post substack youtube

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1259
Problem TLDR
Shuffled list i+n[i] #easy
Intuition
It took me 8 minutes to understand the problem. i - is the index of a result array i+n[i] is the index of a value we take
Approach
- in Kotlin & Rust we have built-in for negative mod:
mod&rem_euclid - in-place solution possible if we store results in a left bits
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(n)\)
Code
// 162ms
fun constructTransformedArray(n: IntArray) =
List(n.size) { n[(it + n[it]).mod(n.size)] }
// 3ms
pub fn construct_transformed_array(n: Vec<i32>) -> Vec<i32> {
(0..n.len()).map(|i|n[((n[i]+i as i32).rem_euclid(n.len() as i32)) as usize]).collect()
}