title: Length Of The Longest Arithmatic Progression In A Sorted Array
signature: function length_of_the_longest_arithmatic_progression_in_a_sorted_array(set_arg0, n)
source_py: def length_of_the_longest_arithmatic_progression_in_a_sorted_array(set, n):
      if (n <= 2):
          return n
      L = [[0 for x in range(n)] for y in range(n)]
      llap = 2
      for i in range(n):
          L[i][(n - 1)] = 2
      for j in range((n - 2), 0, (- 1)):
          i = (j - 1)
          k = (j + 1)
          while ((i >= 0) and (k <= (n - 1))):
              if ((set[i] + set[k]) < (2 * set[j])):
                  k += 1
              elif ((set[i] + set[k]) > (2 * set[j])):
                  L[i][j] = 2
                  i -= 1
              else:
                  L[i][j] = (L[j][k] + 1)
                  llap = max(llap, L[i][j])
                  i -= 1
                  k += 1
                  while (i >= 0):
                      L[i][j] = 2
                      i -= 1
      return llap
source_cpp: int length_of_the_longest_arithmatic_progression_in_a_sorted_array(vector<int> set, int n) {
    if ( n <= 2 ) return n;
    int L [ n ] [ n ];
    int llap = 2;
    for ( int i = 0;
    i < n;
    i ++ ) L [ i ] [ n - 1 ] = 2;
    for ( int j = n - 2;
    j >= 1;
    j -- ) {
      int i = j - 1, k = j + 1;
      while ( i >= 0 && k <= n - 1 ) {
        if ( set [ i ] + set [ k ] < 2 * set [ j ] ) k ++;
        else if ( set [ i ] + set [ k ] > 2 * set [ j ] ) {
          L [ i ] [ j ] = 2, i --;
        }
        else {
          L [ i ] [ j ] = L [ j ] [ k ] + 1;
          llap = max ( llap, L [ i ] [ j ] );
          i --;
          k ++;
        }
      }
      while ( i >= 0 ) {
        L [ i ] [ j ] = 2;
        i --;
      }
    }
    return llap;
  }
question_info.entry_fn_name: length_of_the_longest_arithmatic_progression_in_a_sorted_array
question_info.entry_cls_name: Solution
question_info.test_case_ids: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
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(set_arg0, n, expected)
      local exec_result;
      local status, error_msg = pcall(function () 
          exec_result=PLACEHOLDER_FN_NAME(set_arg0, 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({3, 4, 4, 7, 8, 19, 21, 22, 25, 27, 28, 29, 38, 40, 41, 42, 43, 46, 50, 50, 53, 53, 54, 55, 60, 64, 64, 69, 70, 75, 77, 81, 81, 82, 86, 87, 87, 88, 91, 94, 97}, 27, 4)
      print(string.format("TEST-0...%s",result))
  
      result = driver({40, -6, 50, -18, 42, 78, 38, -90, -44, -42, -86, 78, -68, 2, -32, -20, -44, 54, 80, 54, 70, 26, 82, -14, -74, -20, 74, 82}, 21, 2)
      print(string.format("TEST-1...%s",result))
  
      result = driver({0, 0, 0, 0, 1, 1, 1}, 5, 4)
      print(string.format("TEST-2...%s",result))
  
      result = driver({76, 80}, 1, 1)
      print(string.format("TEST-3...%s",result))
  
      result = driver({-92, -90, -88, -76, -76, -60, -46, -40, -24, -8, -8, -6, 2, 12, 36, 38, 58, 76, 80}, 13, 3)
      print(string.format("TEST-4...%s",result))
  
      result = driver({1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1}, 15, 4)
      print(string.format("TEST-5...%s",result))
  
      result = driver({5, 8, 11, 27, 27, 32, 32, 37, 50, 51, 55, 61, 62, 68, 73, 83}, 8, 3)
      print(string.format("TEST-6...%s",result))
  
      result = driver({52, -74, -32, -64, -52, -60, -70, 36, 70, 40, 40, -18, 90, -70, -82, -64, -8, -6, 36, 4, -58, 62, -96, 78, 36, 90, -70, -6, -84, 24, 84, 32, -90, 36, 70, -60, -56, 78, 48, 34, -16, 80, 82, 58, 14, -6, -8, 76}, 29, 2)
      print(string.format("TEST-7...%s",result))
  
      result = driver({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}, 27, 15)
      print(string.format("TEST-8...%s",result))
  
      result = driver({11, 21, 76, 45, 8, 49, 97, 66, 17, 11, 87, 4, 34, 89, 79, 88, 6, 91, 19, 56, 91, 25, 17, 90, 26, 59, 34, 32, 43, 17, 98, 39, 72, 78, 93, 43}, 26, 2)
      print(string.format("TEST-9...%s",result))
  
  end
  
  main()
question_info.test_list: [{"idx": 0, "outputs": 4, "inputs": {"set_arg0": [3, 4, 4, 7, 8, 19, 21, 22, 25, 27, 28, 29, 38, 40, 41, 42, 43, 46, 50, 50, 53, 53, 54, 55, 60, 64, 64, 69, 70, 75, 77, 81, 81, 82, 86, 87, 87, 88, 91, 94, 97], "n": 27}}, {"idx": 1, "outputs": 2, "inputs": {"set_arg0": [40, -6, 50, -18, 42, 78, 38, -90, -44, -42, -86, 78, -68, 2, -32, -20, -44, 54, 80, 54, 70, 26, 82, -14, -74, -20, 74, 82], "n": 21}}, {"idx": 2, "outputs": 4, "inputs": {"set_arg0": [0, 0, 0, 0, 1, 1, 1], "n": 5}}, {"idx": 3, "outputs": 1, "inputs": {"set_arg0": [76, 80], "n": 1}}, {"idx": 4, "outputs": 3, "inputs": {"set_arg0": [-92, -90, -88, -76, -76, -60, -46, -40, -24, -8, -8, -6, 2, 12, 36, 38, 58, 76, 80], "n": 13}}, {"idx": 5, "outputs": 4, "inputs": {"set_arg0": [1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1], "n": 15}}, {"idx": 6, "outputs": 3, "inputs": {"set_arg0": [5, 8, 11, 27, 27, 32, 32, 37, 50, 51, 55, 61, 62, 68, 73, 83], "n": 8}}, {"idx": 7, "outputs": 2, "inputs": {"set_arg0": [52, -74, -32, -64, -52, -60, -70, 36, 70, 40, 40, -18, 90, -70, -82, -64, -8, -6, 36, 4, -58, 62, -96, 78, 36, 90, -70, -6, -84, 24, 84, 32, -90, 36, 70, -60, -56, 78, 48, 34, -16, 80, 82, 58, 14, -6, -8, 76], "n": 29}}, {"idx": 8, "outputs": 15, "inputs": {"set_arg0": [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], "n": 27}}, {"idx": 9, "outputs": 2, "inputs": {"set_arg0": [11, 21, 76, 45, 8, 49, 97, 66, 17, 11, 87, 4, 34, 89, 79, 88, 6, 91, 19, 56, 91, 25, 17, 90, 26, 59, 34, 32, 43, 17, 98, 39, 72, 78, 93, 43], "n": 26}}]