Advertisement
mdgaziur001

Never Gonna Run Around

Nov 19th, 2021
1,608
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.62 KB | None | 0 0
  1. use std::io;
  2. use std::str::FromStr;
  3. use std::fmt::Debug;
  4.  
  5. struct Scanner {
  6.     buf: Vec<String>
  7. }
  8.  
  9. impl Scanner {
  10.     pub fn new() -> Self {
  11.         Self {
  12.             buf: vec![]
  13.         }
  14.     }
  15.  
  16.     pub fn next<T>(&mut self) -> T
  17.     where
  18.         T: FromStr,
  19.         <T as FromStr>::Err: Debug,
  20.     {
  21.         if !self.buf.is_empty() {
  22.             return self.buf.pop().unwrap().parse::<T>().unwrap();
  23.         }
  24.  
  25.         let mut line = String::new();
  26.         io::stdin().read_line(&mut line).unwrap();
  27.        
  28.         self.buf.extend(line
  29.                         .trim()
  30.                         .split_ascii_whitespace()
  31.                         .map(|i| i.to_string())
  32.                         .collect::<Vec<String>>());
  33.         return self.buf.pop().unwrap().parse::<T>().unwrap()
  34.     }
  35. }
  36.  
  37. fn main() {
  38.     let mut scanner = Scanner::new();
  39.     let testcase: usize = scanner.next();
  40.  
  41.     for _ in 0..testcase {
  42.         let mut array: Vec<i32> = Vec::new();
  43.         let array_size: usize = scanner.next();
  44.        
  45.         for _ in 0..array_size {
  46.             array.push(scanner.next());
  47.         }
  48.  
  49.         if array_size <= 1 {
  50.             println!("{}", array[0]);
  51.             continue;
  52.         }
  53.         array.sort();
  54.         let mut max_difference = 0;
  55.         let mut idx = 0;
  56.         while idx+1 < array_size {
  57.             let diff = (array[idx] - array[idx+1]).abs();
  58.            
  59.             if diff > max_difference {
  60.                 max_difference = diff;
  61.             }
  62.  
  63.             idx += 1;
  64.         }
  65.        
  66.         println!("{}", std::cmp::max(max_difference, array[0]));
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement