title: Search Insert And Delete In A Sorted Array 1
signature: function search_insert_and_delete_in_a_sorted_array_1(arr, n, key, capacity)
source_py: def search_insert_and_delete_in_a_sorted_array_1(arr, n, key, capacity):
      if (n >= capacity):
          return n
      i = (n - 1)
      while ((i >= 0) and (arr[i] > key)):
          arr[(i + 1)] = arr[i]
          i -= 1
      arr[(i + 1)] = key
      return (n + 1)
source_cpp: int search_insert_and_delete_in_a_sorted_array_1(vector<int> arr, int n, int key, int capacity) {
    if ( n >= capacity ) return n;
    int i;
    for ( i = n - 1;
    ( i >= 0 && arr [ i ] > key );
    i -- ) arr [ i + 1 ] = arr [ i ];
    arr [ i + 1 ] = key;
    return ( n + 1 );
  }
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 isCorrect(actual,expected)
  end
  
  function driver(arr, n, key, capacity, expected)
      local exec_result;
      local status, error_msg = pcall(function () 
          exec_result=PLACEHOLDER_FN_NAME(arr, n, key, capacity) 
      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({69}, 0, 0, 0, 0)
      print(string.format("TEST-0...%s",result))
  
      result = driver({-34, -38, -72, 90, -84, -40, 6, -52, -12, 80, -4, -58}, 6, 6, 9, 7)
      print(string.format("TEST-1...%s",result))
  
      result = driver({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1}, 13, 19, 11, 13)
      print(string.format("TEST-2...%s",result))
  
      result = driver({96, 34, 11, 1, 36, 79, 64, 75, 75, 96, 32, 18, 25, 79, 63, 80, 90, 75, 44, 71, 48, 1, 62, 53, 17, 98}, 21, 20, 13, 21)
      print(string.format("TEST-3...%s",result))
  
      result = driver({-98, -92, -92, -84, -82, -80, -80, -74, -70, -60, -48, -42, -36, -34, -34, -34, -30, -28, -16, -6, -2, -2, 2, 12, 14, 20, 24, 40, 46, 50, 60, 66, 70, 72, 78, 82, 88, 92, 94, 94, 96}, 30, 32, 28, 30)
      print(string.format("TEST-4...%s",result))
  
      result = driver({1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0}, 12, 9, 10, 12)
      print(string.format("TEST-5...%s",result))
  
      result = driver({10, 12, 12, 13, 19, 20, 21, 24, 27, 37, 47, 50, 54, 58, 61, 63, 63, 68, 73, 75, 87, 90, 90, 92, 92}, 12, 13, 21, 13)
      print(string.format("TEST-6...%s",result))
  
      result = driver({-74, 62, 74, 92, -38, -28, -26, 4, 88, -68, -76, -20, -4, 12, 72, 6, 42, 36, 88, -96, -80, 90, 80, -26, -36, -72, -62, 38, -20, 40, -10, -22, -20, 38, 82, -84, 8, 26, 86, -26, 44, -72, -70, -16, -22, 18, -16, 76, -50}, 37, 26, 42, 38)
      print(string.format("TEST-7...%s",result))
  
      result = driver({1, 1, 1, 1, 1}, 3, 4, 2, 3)
      print(string.format("TEST-8...%s",result))
  
      result = driver({64, 80, 47, 58, 41, 3, 85, 96, 51, 4, 22, 89, 67, 54, 88, 15, 83, 31, 19, 24, 28, 40, 67, 37, 63, 38, 27, 14, 7, 68}, 23, 24, 25, 24)
      print(string.format("TEST-9...%s",result))
  
  end
  
  main()
question_info.entry_cls_name: Solution
question_info.test_list: [{"idx": 0, "outputs": 0, "inputs": {"arr": [69], "n": 0, "key": 0, "capacity": 0}}, {"idx": 1, "outputs": 7, "inputs": {"arr": [-34, -38, -72, 90, -84, -40, 6, -52, -12, 80, -4, -58], "n": 6, "key": 6, "capacity": 9}}, {"idx": 2, "outputs": 13, "inputs": {"arr": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1], "n": 13, "key": 19, "capacity": 11}}, {"idx": 3, "outputs": 21, "inputs": {"arr": [96, 34, 11, 1, 36, 79, 64, 75, 75, 96, 32, 18, 25, 79, 63, 80, 90, 75, 44, 71, 48, 1, 62, 53, 17, 98], "n": 21, "key": 20, "capacity": 13}}, {"idx": 4, "outputs": 30, "inputs": {"arr": [-98, -92, -92, -84, -82, -80, -80, -74, -70, -60, -48, -42, -36, -34, -34, -34, -30, -28, -16, -6, -2, -2, 2, 12, 14, 20, 24, 40, 46, 50, 60, 66, 70, 72, 78, 82, 88, 92, 94, 94, 96], "n": 30, "key": 32, "capacity": 28}}, {"idx": 5, "outputs": 12, "inputs": {"arr": [1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0], "n": 12, "key": 9, "capacity": 10}}, {"idx": 6, "outputs": 13, "inputs": {"arr": [10, 12, 12, 13, 19, 20, 21, 24, 27, 37, 47, 50, 54, 58, 61, 63, 63, 68, 73, 75, 87, 90, 90, 92, 92], "n": 12, "key": 13, "capacity": 21}}, {"idx": 7, "outputs": 38, "inputs": {"arr": [-74, 62, 74, 92, -38, -28, -26, 4, 88, -68, -76, -20, -4, 12, 72, 6, 42, 36, 88, -96, -80, 90, 80, -26, -36, -72, -62, 38, -20, 40, -10, -22, -20, 38, 82, -84, 8, 26, 86, -26, 44, -72, -70, -16, -22, 18, -16, 76, -50], "n": 37, "key": 26, "capacity": 42}}, {"idx": 8, "outputs": 3, "inputs": {"arr": [1, 1, 1, 1, 1], "n": 3, "key": 4, "capacity": 2}}, {"idx": 9, "outputs": 24, "inputs": {"arr": [64, 80, 47, 58, 41, 3, 85, 96, 51, 4, 22, 89, 67, 54, 88, 15, 83, 31, 19, 24, 28, 40, 67, 37, 63, 38, 27, 14, 7, 68], "n": 23, "key": 24, "capacity": 25}}]
question_info.extension: lua
question_info.entry_fn_name: search_insert_and_delete_in_a_sorted_array_1
question_info.test_case_ids: 0 1 2 3 4 5 6 7 8 9
question_info.commands: lua __FILENAME__