Created
May 3, 2020 16:33
-
-
Save Lazhari/e43fb5d5ec12dc5962c60ac28379e6b4 to your computer and use it in GitHub Desktop.
Print all sub arrays with 0 sum using rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn print_all_sub_arrays(arr: Vec<i128>) -> Vec<Vec<i128>> { | |
let mut sub_arrays:Vec<Vec<i128>> = Vec::new(); | |
for i in 0..arr.len() { | |
let mut sum = 0; | |
for j in i..arr.len() { | |
sum += arr[j]; | |
if sum == 0 { | |
sub_arrays.push(arr[i..=j].to_vec()); | |
} | |
} | |
} | |
return sub_arrays; | |
} | |
fn main() { | |
let arr = vec![4,2,-3,-1,0,4]; | |
let sub_arrays = print_all_sub_arrays(arr); | |
println!("{:?}", sub_arrays); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment