title: Length Of The Longest Arithmatic Progression In A Sorted Array
signature: length_of_the_longest_arithmatic_progression_in_a_sorted_array <- function(set_arg0, n) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(set_arg0, n, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(set_arg0, n), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          list(3L, 4L, 4L, 7L, 8L, 19L, 21L, 22L, 25L, 27L, 28L, 29L, 38L, 40L, 41L, 42L, 43L, 46L, 50L, 50L, 53L, 53L, 54L, 55L, 60L, 64L, 64L, 69L, 70L, 75L, 77L, 81L, 81L, 82L, 86L, 87L, 87L, 88L, 91L, 94L, 97L), 27L, 
          4L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(40L, -6L, 50L, -18L, 42L, 78L, 38L, -90L, -44L, -42L, -86L, 78L, -68L, 2L, -32L, -20L, -44L, 54L, 80L, 54L, 70L, 26L, 82L, -14L, -74L, -20L, 74L, 82L), 21L, 
          2L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(0L, 0L, 0L, 0L, 1L, 1L, 1L), 5L, 
          4L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(76L, 80L), 1L, 
          1L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-92L, -90L, -88L, -76L, -76L, -60L, -46L, -40L, -24L, -8L, -8L, -6L, 2L, 12L, 36L, 38L, 58L, 76L, 80L), 13L, 
          3L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(1L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 1L, 1L), 15L, 
          4L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(5L, 8L, 11L, 27L, 27L, 32L, 32L, 37L, 50L, 51L, 55L, 61L, 62L, 68L, 73L, 83L), 8L, 
          3L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(52L, -74L, -32L, -64L, -52L, -60L, -70L, 36L, 70L, 40L, 40L, -18L, 90L, -70L, -82L, -64L, -8L, -6L, 36L, 4L, -58L, 62L, -96L, 78L, 36L, 90L, -70L, -6L, -84L, 24L, 84L, 32L, -90L, 36L, 70L, -60L, -56L, 78L, 48L, 34L, -16L, 80L, 82L, 58L, 14L, -6L, -8L, 76L), 29L, 
          2L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 27L, 
          15L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(11L, 21L, 76L, 45L, 8L, 49L, 97L, 66L, 17L, 11L, 87L, 4L, 34L, 89L, 79L, 88L, 6L, 91L, 19L, 56L, 91L, 25L, 17L, 90L, 26L, 59L, 34L, 32L, 43L, 17L, 98L, 39L, 72L, 78L, 93L, 43L), 26L, 
          2L
      )))
  
  }
  
  main()
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;
  }