daily pastebin goal
57%
SHARE
TWEET

Untitled

a guest Jun 10th, 2018 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "math"
  6.     "sort"
  7. )
  8.  
  9. func main() {
  10.     res := Solution([]int{8, 24, 3, 20, 1, 17})
  11.     fmt.Println(res)
  12.  
  13.     res = Solution([]int{7, 21, 3, 42, 3, 7})
  14.     fmt.Println(res)
  15. }
  16.  
  17. func Solution(A []int) int {
  18.     minVal := 999999.0
  19.  
  20.     sort.Ints(A)
  21.     lastVal := 0
  22.     for idx, val := range A {
  23.         if idx > 0 {
  24.             subs := math.Abs(float64(val) - float64(lastVal))
  25.             if subs < minVal {
  26.                 minVal = subs
  27.             }
  28.         }
  29.         lastVal = val
  30.     }
  31.  
  32.     return int(minVal)
  33. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top