title: Efficient Search In An Array Where Difference Between Adjacent Is 1
signature: function efficient_search_in_an_array_where_difference_between_adjacent_is_1(arr, n, x)
source_py: def efficient_search_in_an_array_where_difference_between_adjacent_is_1(arr, n, x):
      i = 0
      while (i <= (n - 1)):
          if (arr[i] == x):
              return i
          i += abs((arr[i] - x))
      return (- 1)
source_cpp: int efficient_search_in_an_array_where_difference_between_adjacent_is_1(vector<int> arr, int n, int x) {
    int i = 0;
    while ( i <= n - 1 ) {
      if ( arr [ i ] == x ) return i;
      i += abs ( arr [ i ] - x );
    }
    return - 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, x, expected)
      local exec_result;
      local status, error_msg = pcall(function () 
          exec_result=PLACEHOLDER_FN_NAME(arr, n, x) 
      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({5, 4, 5, 6, 5, 4, 3, 2}, 29, 6, 3)
      print(string.format("TEST-0...%s",result))
  
      result = driver({5, 4, 5, 6, 5, 4, 3, 2}, 19, 3, 6)
      print(string.format("TEST-1...%s",result))
  
      result = driver({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 37, 1, 14)
      print(string.format("TEST-2...%s",result))
  
      result = driver({50, 51, 52, 51, 50, 49, 48}, 17, 49, 5)
      print(string.format("TEST-3...%s",result))
  
      result = driver({-86, -68, -32, -6, 6, 10, 30, 34, 58, 92}, 6, 6, -1)
      print(string.format("TEST-4...%s",result))
  
      result = driver({1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0}, 27, 22, -1)
      print(string.format("TEST-5...%s",result))
  
      result = driver({58}, 0, 0, -1)
      print(string.format("TEST-6...%s",result))
  
      result = driver({-64, 78, 58, 36, 48, 80, -80, 74, -98, 46, -48, 24, 80, 72, 90, -46, 14, 68, 38, 58, -54, 80, 44, -62, 34, -28, 92, 84, 90, 44, -26, 88, 18, 22, -32, 54, 58, 92}, 24, 34, -1)
      print(string.format("TEST-7...%s",result))
  
      result = driver({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 35, 1, 21)
      print(string.format("TEST-8...%s",result))
  
      result = driver({5}, 0, 0, -1)
      print(string.format("TEST-9...%s",result))
  
  end
  
  main()
question_info.entry_cls_name: Solution
question_info.test_list: [{"idx": 0, "outputs": 3, "inputs": {"arr": [5, 4, 5, 6, 5, 4, 3, 2], "n": 29, "x": 6}}, {"idx": 1, "outputs": 6, "inputs": {"arr": [5, 4, 5, 6, 5, 4, 3, 2], "n": 19, "x": 3}}, {"idx": 2, "outputs": 14, "inputs": {"arr": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], "n": 37, "x": 1}}, {"idx": 3, "outputs": 5, "inputs": {"arr": [50, 51, 52, 51, 50, 49, 48], "n": 17, "x": 49}}, {"idx": 4, "outputs": -1, "inputs": {"arr": [-86, -68, -32, -6, 6, 10, 30, 34, 58, 92], "n": 6, "x": 6}}, {"idx": 5, "outputs": -1, "inputs": {"arr": [1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0], "n": 27, "x": 22}}, {"idx": 6, "outputs": -1, "inputs": {"arr": [58], "n": 0, "x": 0}}, {"idx": 7, "outputs": -1, "inputs": {"arr": [-64, 78, 58, 36, 48, 80, -80, 74, -98, 46, -48, 24, 80, 72, 90, -46, 14, 68, 38, 58, -54, 80, 44, -62, 34, -28, 92, 84, 90, 44, -26, 88, 18, 22, -32, 54, 58, 92], "n": 24, "x": 34}}, {"idx": 8, "outputs": 21, "inputs": {"arr": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], "n": 35, "x": 1}}, {"idx": 9, "outputs": -1, "inputs": {"arr": [5], "n": 0, "x": 0}}]
question_info.extension: lua
question_info.entry_fn_name: efficient_search_in_an_array_where_difference_between_adjacent_is_1
question_info.test_case_ids: 0 1 2 3 4 5 6 7 8 9
question_info.commands: lua __FILENAME__