title: Median Of Two Sorted Arrays
signature: function median_of_two_sorted_arrays(ar1, ar2, n)
source_py: def median_of_two_sorted_arrays(ar1, ar2, n):
      i = 0
      j = 0
      m1 = (- 1)
      m2 = (- 1)
      count = 0
      while (count < (n + 1)):
          count += 1
          if (i == n):
              m1 = m2
              m2 = ar2[0]
              break
          elif (j == n):
              m1 = m2
              m2 = ar1[0]
              break
          if (ar1[i] < ar2[j]):
              m1 = m2
              m2 = ar1[i]
              i += 1
          else:
              m1 = m2
              m2 = ar2[j]
              j += 1
      return ((m1 + m2) / 2)
source_cpp: int median_of_two_sorted_arrays(vector<int> ar1, vector<int> ar2, int n) {
    int i = 0;
    int j = 0;
    int count;
    int m1 = - 1, m2 = - 1;
    for ( count = 0;
    count <= n;
    count ++ ) {
      if ( i == n ) {
        m1 = m2;
        m2 = ar2 [ 0 ];
        break;
      }
      else if ( j == n ) {
        m1 = m2;
        m2 = ar1 [ 0 ];
        break;
      }
      if ( ar1 [ i ] < ar2 [ j ] ) {
        m1 = m2;
        m2 = ar1 [ i ];
        i ++;
      }
      else {
        m1 = m2;
        m2 = ar2 [ j ];
        j ++;
      }
    }
    return ( m1 + m2 ) / 2;
  }
question_info.test_code: 
  
  -- SOLUTION CODE
  -- ============================================
  PLACEHOLDER_CODE_BODY
  
  -- TESTING CODE 
  -- ============================================
  function isCorrect(actual, expected)
      if type(expected) ~= type(actual) then
          return false
      end
      
      if type(expected) == 'table' then
          for k,v_expected in pairs(expected) do
              
              if actual[k] == nil then
                  return false
              end
              local v_actual = actual[k]
              if not isCorrect(v_actual, v_expected) then
                  return false
              end
              actual[k]=nil
          end
      
          if next(actual) then 
              return false
          end
          return true
      else
          return actual == expected
      end
  end
  
  function validateSolution(actual, expected)
      return math.abs(actual - expected) < 1e-06
  end
  
  function driver(ar1, ar2, n, expected)
      local exec_result;
      local status, error_msg = pcall(function () 
          exec_result=PLACEHOLDER_FN_NAME(ar1, ar2, n) 
      end)
      if status then
          if validateSolution(exec_result,expected) then
              return "PASSED"
          end
          return "FAILED"
      else
          return string.format("ERROR=%s",string.gsub(error_msg,".*:[%d]+: ",""))
      end
  end
      
  function main()
      result = driver({2, 6, 18, 21, 23, 27, 44, 44, 69, 72, 78, 88, 90, 98}, {6, 12, 16, 18, 26, 34, 48, 48, 49, 56, 61, 79, 81, 89}, 12, 39.0)
      print(string.format("TEST-0...%s",result))
  
      result = driver({90, 54, 24, -10, -84, -74, 58, 96, -28, -92, -18, 90, 70, -60, 72, 78, 10, 42, -2, -18, -38, -16, 18, -86, 40, -46, -38, 66, 20, -16, 48}, {-72, -62, 14, -58, 70, 54, 88, -40, -94, 4, 60, -16, -38, -98, -70, -46, 66, 42, 26, 36, 56, -4, 32, 30, -46, -42, -72, 44, 16, 4, 24}, 16, 22.0)
      print(string.format("TEST-1...%s",result))
  
      result = driver({0, 1, 1}, {0, 1, 1}, 2, 0.5)
      print(string.format("TEST-2...%s",result))
  
      result = driver({53, 17, 94, 21, 16, 75, 67, 51, 44, 71, 65, 82}, {98, 50, 8, 11, 80, 41, 59, 24, 94, 41, 75, 78}, 10, 84.5)
      print(string.format("TEST-3...%s",result))
  
      result = driver({-96, -92, -80, -68, -64, -64, -60, -56, -52, -50, -50, -22, -20, -4, -2, 0, 6, 20, 22, 28, 38, 40, 48, 50, 56, 58, 64, 64, 80, 82, 90, 92, 92, 92}, {-88, -72, -72, -58, -54, -50, -48, -34, -24, -14, -14, -14, -10, -6, 4, 12, 16, 18, 26, 30, 32, 34, 40, 46, 52, 54, 58, 62, 62, 72, 82, 82, 92, 98}, 25, -12.0)
      print(string.format("TEST-4...%s",result))
  
      result = driver({0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1}, {1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1}, 40, 0.5)
      print(string.format("TEST-5...%s",result))
  
      result = driver({8, 15, 17, 19, 21, 32, 34, 38, 41, 41, 49, 49, 51, 54, 54, 56, 56, 57, 59, 63, 70, 74, 79, 79, 84, 84, 86, 88, 89, 93, 98}, {5, 6, 17, 18, 22, 29, 32, 33, 36, 44, 45, 47, 59, 59, 60, 65, 67, 68, 69, 71, 72, 76, 78, 81, 84, 85, 85, 86, 86, 87, 92}, 29, 56.5)
      print(string.format("TEST-6...%s",result))
  
      result = driver({96, -42, -94, -46, -68, 76, 8, 16, -54, -94, 76, 24, 94, 10, 34, 78, -30, 0, -52, 80, 98, -58, 92, 12, 26, 64}, {88, 78, -26, 10, 84, 34, 56, -8, -30, 46, 48, 20, 26, -78, 96, 44, 92, -44, -86, 24, -58, -96, -86, -12, -98, 18}, 17, 94.0)
      print(string.format("TEST-7...%s",result))
  
      result = driver({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1}, 17, 0.0)
      print(string.format("TEST-8...%s",result))
  
      result = driver({61, 69, 66, 3}, {39, 84, 97, 15}, 3, 67.5)
      print(string.format("TEST-9...%s",result))
  
  end
  
  main()
question_info.entry_cls_name: Solution
question_info.test_list: [{"idx": 0, "outputs": 39.0, "inputs": {"ar1": [2, 6, 18, 21, 23, 27, 44, 44, 69, 72, 78, 88, 90, 98], "ar2": [6, 12, 16, 18, 26, 34, 48, 48, 49, 56, 61, 79, 81, 89], "n": 12}}, {"idx": 1, "outputs": 22.0, "inputs": {"ar1": [90, 54, 24, -10, -84, -74, 58, 96, -28, -92, -18, 90, 70, -60, 72, 78, 10, 42, -2, -18, -38, -16, 18, -86, 40, -46, -38, 66, 20, -16, 48], "ar2": [-72, -62, 14, -58, 70, 54, 88, -40, -94, 4, 60, -16, -38, -98, -70, -46, 66, 42, 26, 36, 56, -4, 32, 30, -46, -42, -72, 44, 16, 4, 24], "n": 16}}, {"idx": 2, "outputs": 0.5, "inputs": {"ar1": [0, 1, 1], "ar2": [0, 1, 1], "n": 2}}, {"idx": 3, "outputs": 84.5, "inputs": {"ar1": [53, 17, 94, 21, 16, 75, 67, 51, 44, 71, 65, 82], "ar2": [98, 50, 8, 11, 80, 41, 59, 24, 94, 41, 75, 78], "n": 10}}, {"idx": 4, "outputs": -12.0, "inputs": {"ar1": [-96, -92, -80, -68, -64, -64, -60, -56, -52, -50, -50, -22, -20, -4, -2, 0, 6, 20, 22, 28, 38, 40, 48, 50, 56, 58, 64, 64, 80, 82, 90, 92, 92, 92], "ar2": [-88, -72, -72, -58, -54, -50, -48, -34, -24, -14, -14, -14, -10, -6, 4, 12, 16, 18, 26, 30, 32, 34, 40, 46, 52, 54, 58, 62, 62, 72, 82, 82, 92, 98], "n": 25}}, {"idx": 5, "outputs": 0.5, "inputs": {"ar1": [0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1], "ar2": [1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1], "n": 40}}, {"idx": 6, "outputs": 56.5, "inputs": {"ar1": [8, 15, 17, 19, 21, 32, 34, 38, 41, 41, 49, 49, 51, 54, 54, 56, 56, 57, 59, 63, 70, 74, 79, 79, 84, 84, 86, 88, 89, 93, 98], "ar2": [5, 6, 17, 18, 22, 29, 32, 33, 36, 44, 45, 47, 59, 59, 60, 65, 67, 68, 69, 71, 72, 76, 78, 81, 84, 85, 85, 86, 86, 87, 92], "n": 29}}, {"idx": 7, "outputs": 94.0, "inputs": {"ar1": [96, -42, -94, -46, -68, 76, 8, 16, -54, -94, 76, 24, 94, 10, 34, 78, -30, 0, -52, 80, 98, -58, 92, 12, 26, 64], "ar2": [88, 78, -26, 10, 84, 34, 56, -8, -30, 46, 48, 20, 26, -78, 96, 44, 92, -44, -86, 24, -58, -96, -86, -12, -98, 18], "n": 17}}, {"idx": 8, "outputs": 0.0, "inputs": {"ar1": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1], "ar2": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1], "n": 17}}, {"idx": 9, "outputs": 67.5, "inputs": {"ar1": [61, 69, 66, 3], "ar2": [39, 84, 97, 15], "n": 3}}]
question_info.extension: lua
question_info.entry_fn_name: median_of_two_sorted_arrays
question_info.test_case_ids: 0 1 2 3 4 5 6 7 8 9
question_info.commands: lua __FILENAME__