title: Length Of The Longest Arithmatic Progression In A Sorted Array
signature: class Solution {
  
      public int LengthOfTheLongestArithmaticProgressionInASortedArray(List<int> set_arg0, int n) {
test_code: using System;
  using System.Numerics;
  using System.Diagnostics;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Security.Cryptography;
  using System.Web.UI;
  using System.Web.Script.Serialization;
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  namespace EvaluatePrediction {
      class TestRunner {
          public bool ValidateSolution(int actual, int expected){
              return new JavaScriptSerializer().Serialize(actual) == new JavaScriptSerializer().Serialize(expected);
          }
  
          public string driver(List<int> set_arg0, int n, int expected){
              try{
                PLACEHOLDER_CLS_NAME solution_cls = new PLACEHOLDER_CLS_NAME();
                int pred_result = solution_cls.PLACEHOLDER_FN_NAME(set_arg0, n); 
                if (ValidateSolution(
                      pred_result,
                      expected)){
                  return "PASSED";
                }
                return "FAILED";
              } 
              catch (Exception exception_obj)
              {
                return exception_obj.GetType().FullName;
              }
          }
          static void Main(string[] args) {
              TestRunner runner = new TestRunner();
              String result;
              result = runner.driver(
                  new List<int>{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
              );
              Console.WriteLine(string.Format(
                  "TEST-0...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{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
              );
              Console.WriteLine(string.Format(
                  "TEST-1...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{0, 0, 0, 0, 1, 1, 1}, 5, 
                  4
              );
              Console.WriteLine(string.Format(
                  "TEST-2...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{76, 80}, 1, 
                  1
              );
              Console.WriteLine(string.Format(
                  "TEST-3...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{-92, -90, -88, -76, -76, -60, -46, -40, -24, -8, -8, -6, 2, 12, 36, 38, 58, 76, 80}, 13, 
                  3
              );
              Console.WriteLine(string.Format(
                  "TEST-4...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1}, 15, 
                  4
              );
              Console.WriteLine(string.Format(
                  "TEST-5...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{5, 8, 11, 27, 27, 32, 32, 37, 50, 51, 55, 61, 62, 68, 73, 83}, 8, 
                  3
              );
              Console.WriteLine(string.Format(
                  "TEST-6...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{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
              );
              Console.WriteLine(string.Format(
                  "TEST-7...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{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
              );
              Console.WriteLine(string.Format(
                  "TEST-8...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{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
              );
              Console.WriteLine(string.Format(
                  "TEST-9...{0}",
                  result)
              );
          }
      }
  }
  
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;
  }