SHARE
TWEET

Untitled

a guest Nov 15th, 2019 95 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. )
  7.  
  8. func main() {
  9.     fmt.Println(get_out_of_order_value([]int64{}))
  10.     fmt.Println(get_out_of_order_value([]int64{6}))
  11.     fmt.Println(get_out_of_order_value([]int64{3, 1, 2, 6, 9}))
  12.     fmt.Println(get_out_of_order_value([]int64{233, 87, 4334, 88, 99424, 123}))
  13.     fmt.Println(get_out_of_order_value([]int64{233, 87, 4, 88, 99424, 123}))
  14. }
  15.  
  16. func get_out_of_order_value(arr []int64) int64 {
  17.     if len(arr) < 2 { return -1 }
  18.  
  19.     min := int64(math.MaxInt64)
  20.  
  21.     for i := range arr {
  22.         if arr[i] < min { min = arr[i] }
  23.     }
  24.  
  25.     for i, val := range arr {
  26.         nval := val - min
  27.  
  28.         for {
  29.             if int64(i) == nval || nval > int64(len(arr)) - 1 { break }
  30.             arr[i], arr[nval] = arr[nval], arr[i]
  31.             nval = arr[i] - min
  32.         }
  33.     }
  34.  
  35.     for i := 0; i < len(arr) - 1; i++ {
  36.         if int64(i) == arr[i] - min { continue }
  37.         return arr[i - 1] + 1
  38.     }
  39.  
  40.     return -1
  41. }
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