Skip to content

Instantly share code, notes, and snippets.

@Lazhari
Created May 3, 2020 16:33
Show Gist options
  • Save Lazhari/e43fb5d5ec12dc5962c60ac28379e6b4 to your computer and use it in GitHub Desktop.
Save Lazhari/e43fb5d5ec12dc5962c60ac28379e6b4 to your computer and use it in GitHub Desktop.
Print all sub arrays with 0 sum using rust
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