This returns a vector each element of which is squared and then whole vector is sorted in non-decreasing order

Seems not so efficient

Why do you have the limit on the length of the vector? One thing to note is that you don't have to create a new vector. You can modify the one you got:

pub fn sorted_squares(mut nums: Vec<i32>) -> Vec<i32> {
    for val in &mut nums {
        let current_value = *val;
        *val = current_value * current_value;
    }
    
    nums.sort_unstable();
    
    nums
}
2 Likes

Thanks, that limit is due to restriction/constrain in question

this for loop and not making another vector causes it to be 4ms faster :slight_smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.