input: Write a function to find the closest value to a given number in a given array.
  assert closest_num([5, 4, 8, 7], 5)==5
  assert closest_num([3, 6, 7, 8, 9], 4)==3
  assert closest_num([1, 2, 3, 4, 5], 6)==5
  assert closest_num([5, 7, 4, 6], 8)==7
  
  def closest_num(arr, num):
      arr.sort()
      n = len(arr)
      left = 0
      right = n - 1
   
      if arr[right] <= num:
          return arr[right]
      elif arr[left] >= num:
          return arr[left]
      while right-left > 1:
          mid = (right+left)//2
          if arr[mid] == num:
              return arr[mid]
          elif arr[mid] > num:
              right = mid
          else:
              left = mid
      return sorted([arr[right], arr[left]])[0]
output: def closest_num(arr, num):
      closest = arr[0]
      for value in arr:
          if abs(value - num) < abs(closest - num):
              closest = value
      return closest