Skip to content

Instantly share code, notes, and snippets.

@anataliocs
Created June 16, 2024 04:07
Show Gist options
  • Save anataliocs/479bc94ce1ddcb963546a8bae58b4fa6 to your computer and use it in GitHub Desktop.
Save anataliocs/479bc94ce1ddcb963546a8bae58b4fa6 to your computer and use it in GitHub Desktop.
Rust minmax function with array manipulation
fn miniMaxSum(arr: &[i32]) {
let len = arr.len();
let mut list: Vec<i64> = vec![];
for n in 0..len {
let &sum = &arr.iter()
.enumerate()
.filter(|(index, &y)| !index.eq(&n))
.map(|(i, &y)| i64::from(y))
.sum::<i64>();
list.push(sum);
}
let min = list.iter()
.min_by(|x, y| x.cmp(y))
.unwrap();
let max = list.iter()
.max_by(|x, y| x.cmp(y))
.unwrap();
println!("{min} {max}");
}
fn main() {
let stdin = io::stdin();
let mut stdin_iterator = stdin.lock().lines();
let arr: Vec<i32> = stdin_iterator.next().unwrap().unwrap()
.trim_end()
.split(' ')
.map(|s| s.to_string().parse::<i32>().unwrap())
.collect();
miniMaxSum(&arr);
}
@anataliocs
Copy link
Author

@anataliocs
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment