...

/

Solution Review: Find Two Numbers that Add Up to the Given Value

Solution Review: Find Two Numbers that Add Up to the Given Value

This review provides a detailed analysis of the different ways to solve the "Find Two Numbers that Add Up to the Given Value" challenge.

Solution #1: Brute force

Press + to interact
fn find_sum(arr: &[i32], value: i32) -> [i32;2]{
for x in 0..arr.len(){
for j in x+1..arr.len(){
if arr[x]+arr[j] == value{
return [arr[x], arr[j]];
}
}
}
return [-1,-1];
}

This is the most time-intensive but intuitive solution. Traverse the whole array of the given size for each element in the array, and check if any of the two elements add up to the given number value. Use a nested for-loop, and iterate over the entire array for each element.

Time complexity

Since you iterated over the entire array (size nn) n ...

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