programming challenge – Solution to LeetCode Two Sum problem in Rust

Proper name and description

Rename the question and give it proper description and tags according to https://codereview.stackexchange.com/help/how-to-ask

Indent the code

Unindented code looks ugly. Moreover, it has bad readability. Compare this

    if status {
        result
    } else {
        panic!("Not found");
    }

with your code. See the difference? (Returning) result and panicking happen on the same level. Else is the counterpart of if, not its internal structure. I don’t need to look for curly brackets to say that – indentation does it. You’ll save your own time indenting the code.

Cleanup

You’ve done some debugging output. It’s ok (btw check out the dbg! macro). But once the code is tested you don’t need it anymore. It’s ok to have commented out lines while debugging, but when it’s done – remove them. Clean code is much better to read.
The same goes for unused variables. Rust even gives you a warning for that – don’t ignore the warnings!

Validation and algorithm separation

While not always possible, it is a hood habit to validate data before the algorithm begins. I don’t think you need validation here (it is stated that input data will be ok), but if you still want to validate – do it on the beginning. To have the same loop for validation and for searching reduces the readability. Also recheck the validation conditions – it looks like something is wrong there.

Labels

Go To statement considered harmful. Yes, this is break statement, which is much better, but once again – could you do any better? Yes, of course – when the answer is found, you can simply return it! No need to have labels (‘inner is not needed even now) and status variable! That’s simple!
Also in this case you should not create a result variable – just construct it on return.

Else after return/panic

Sometimes it’s good, especially if you want to show that something else could happen instead of returning (like logging the error). Sometimes not. Right here it’s increasing nesting and can be omitted.

Continue at the end of the loop

Unnecessary, the loop will continue anyway.

Unnecessary complication and includes

Try_into? YAGNI. You’ve just validated the data, indexes can’t be out of 0..100_000 (btw check out this constraint – it looks like something wrong with it), so simple as is enough:

result.push(i as i32)

So, the code now goes as

impl Solution {
    pub fn two_sum(nums: Vec<i32>, target: i32) ->Vec<i32> {
        if (nums.len() < 2 || nums.len() > 10_0000) || (target < -1000_000_000 || target > 1000_000_000){
            panic!("Wrong input data");
        }
        for &val in nums.iter() {
            if val < -100_0000_000 || val > 1000_000_000{
                panic!("Too large or too small value in vec");
            }
        }
            
        for (i, val_i) in nums.iter().enumerate() {
            for j in i+1..nums.len() {
                if val_i + nums(j) == target {
                    return vec!(i as i32, j as i32);
                }
            }
        }   
        panic!("Not found");
    }
}

Choosing better algorithm

You have $O(n^2)$ complexity: n for choosing an element to check and n for looking up for its counterpart. You can do something with …Set or …Map, but the most obvious way is to build a sorted Vec ($O(n ln(n))$), then move from the both sides (i increasing from 0 if v(i) + v(j) is lower than the target, j decreasing from nums.len() - 1 if v(i) + v(j) is higher) until a sum is found, and then search for indexes in the nums ($O(n)$).