title: Maximum Distance Two Occurrences Element Array
signature: function maximum_distance_two_occurrences_element_array(arr, n)
source_py: def maximum_distance_two_occurrences_element_array(arr, n):
      mp = {
          
      }
      maxDict = 0
      for i in range(n):
          if (arr[i] not in mp.keys()):
              mp[arr[i]] = i
          else:
              maxDict = max(maxDict, (i - mp[arr[i]]))
      return maxDict
source_cpp: int maximum_distance_two_occurrences_element_array(vector<int> arr, int n) {
    unordered_map < int, int > mp;
    int max_dist = 0;
    for ( int i = 0;
    i < n;
    i ++ ) {
      if ( mp . find ( arr [ i ] ) == mp . end ( ) ) mp [ arr [ i ] ] = i;
      else max_dist = max ( max_dist, i - mp [ arr [ i ] ] );
    }
    return max_dist;
  }
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, expected)
      local exec_result;
      local status, error_msg = pcall(function () 
          exec_result=PLACEHOLDER_FN_NAME(arr, 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({1, 20, 25, 28, 29, 31, 34, 35, 38, 39, 41, 43, 46, 55, 56, 60, 65, 66, 74, 77, 79, 80, 81, 83, 84, 88, 88, 88, 90, 91, 99}, 27, 1)
      print(string.format("TEST-0...%s",result))
  
      result = driver({26, 14, 56, 84, -56, -84, -98, 12, -78, 18, -42, 58, 46, -66, -46, 66, 98, 34, -16, 8, -20, 66, 74, 26, 42, -84, 38, 86, 14, 86, 26, -42, -30, 6, -54, -76, -66, 18, 58, 66, 74, -62, 8, -42, 62, -14, -90, 98, -24}, 34, 30)
      print(string.format("TEST-1...%s",result))
  
      result = driver({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 11, 10)
      print(string.format("TEST-2...%s",result))
  
      result = driver({69, 87}, 1, 0)
      print(string.format("TEST-3...%s",result))
  
      result = driver({-98, -88, -86, -62, -52, -26, -24, -20, -12, 6, 8, 26, 36, 40, 42, 54, 68, 72, 84, 92, 94, 98, 98}, 12, 0)
      print(string.format("TEST-4...%s",result))
  
      result = driver({1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0}, 29, 28)
      print(string.format("TEST-5...%s",result))
  
      result = driver({11, 16, 17, 19, 20, 21, 21, 22, 27, 37, 45, 49, 64, 77, 81, 85, 96}, 9, 1)
      print(string.format("TEST-6...%s",result))
  
      result = driver({-20, 0, 18, -96}, 2, 0)
      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}, 22, 20)
      print(string.format("TEST-8...%s",result))
  
      result = driver({54, 5, 98, 71, 9, 34, 60, 28, 58, 66, 28, 45, 4}, 10, 0)
      print(string.format("TEST-9...%s",result))
  
  end
  
  main()
question_info.entry_cls_name: Solution
question_info.test_list: [{"idx": 0, "outputs": 1, "inputs": {"arr": [1, 20, 25, 28, 29, 31, 34, 35, 38, 39, 41, 43, 46, 55, 56, 60, 65, 66, 74, 77, 79, 80, 81, 83, 84, 88, 88, 88, 90, 91, 99], "n": 27}}, {"idx": 1, "outputs": 30, "inputs": {"arr": [26, 14, 56, 84, -56, -84, -98, 12, -78, 18, -42, 58, 46, -66, -46, 66, 98, 34, -16, 8, -20, 66, 74, 26, 42, -84, 38, 86, 14, 86, 26, -42, -30, 6, -54, -76, -66, 18, 58, 66, 74, -62, 8, -42, 62, -14, -90, 98, -24], "n": 34}}, {"idx": 2, "outputs": 10, "inputs": {"arr": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], "n": 11}}, {"idx": 3, "outputs": 0, "inputs": {"arr": [69, 87], "n": 1}}, {"idx": 4, "outputs": 0, "inputs": {"arr": [-98, -88, -86, -62, -52, -26, -24, -20, -12, 6, 8, 26, 36, 40, 42, 54, 68, 72, 84, 92, 94, 98, 98], "n": 12}}, {"idx": 5, "outputs": 28, "inputs": {"arr": [1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0], "n": 29}}, {"idx": 6, "outputs": 1, "inputs": {"arr": [11, 16, 17, 19, 20, 21, 21, 22, 27, 37, 45, 49, 64, 77, 81, 85, 96], "n": 9}}, {"idx": 7, "outputs": 0, "inputs": {"arr": [-20, 0, 18, -96], "n": 2}}, {"idx": 8, "outputs": 20, "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], "n": 22}}, {"idx": 9, "outputs": 0, "inputs": {"arr": [54, 5, 98, 71, 9, 34, 60, 28, 58, 66, 28, 45, 4], "n": 10}}]
question_info.extension: lua
question_info.entry_fn_name: maximum_distance_two_occurrences_element_array
question_info.test_case_ids: 0 1 2 3 4 5 6 7 8 9
question_info.commands: lua __FILENAME__