...

/

Solution Review : Rearrange Positive & Negative Values

Solution Review : Rearrange Positive & Negative Values

This review provides a detailed analysis of the different ways to solve the "Rearrange Positive & Negative Values" challenge.

Solution #1: Using new array #

Press + to interact
fn rearrange(arr: &Vec<i32>) -> Vec<i32> {
let mut negative_arr: Vec<i32> = vec![];
// Fill the new vector with negative values
for x in 0..arr.len(){
if arr[x] < 0 {
negative_arr.push(arr[x]);
}
}
// Fill the vector with left over positive values
for x in 0..arr.len(){
if arr[x] > 0 {
negative_arr.push(arr[x]);
}
}
// Return the vector
negative_arr
}

In this solution, iterate over the entire given array, and copy all negative numbers to a newly created array, and then copy positive ones to this array.

Time complexity

Since the given array is iterated over twice, the time complexity of this solution is ...

Access this course and 1400+ top-rated courses and projects.