title: Length Of The Longest Arithmatic Progression In A Sorted Array
signature: def lengthOfTheLongestArithmaticProgressionInASortedArray(set_arg0: List[Int], n: Int) = {
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;
  }