main.go 710 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "strconv"
  7. "strings"
  8. )
  9. func main() {
  10. var input = bufio.NewScanner(os.Stdin)
  11. var output int
  12. var digits []int
  13. for input.Scan() {
  14. line := input.Text()
  15. digits = []int{}
  16. values := strings.Split(line, " ")
  17. for _, v := range values {
  18. d, _ := strconv.Atoi(v)
  19. digits = append(digits, d)
  20. }
  21. output = missingNumber(digits)
  22. fmt.Println(output)
  23. }
  24. }
  25. func missingNumber(nums []int) int {
  26. var length = len(nums)
  27. if length-1 == nums[length-1] {
  28. return length
  29. }
  30. var left, right = 0, len(nums) - 1
  31. var mid int
  32. for left < right {
  33. mid = (left + right) / 2
  34. if nums[mid] > mid {
  35. right = mid
  36. } else {
  37. left = mid + 1
  38. }
  39. }
  40. return left
  41. }