Longest Arithmetic Progression | DP | Returns length of the longest AP subset in a given set ; Driver Code
function Solution ( A ) { let ans = 2 ; let n = A . length ; if ( n <= 2 ) { return n ; } let llap = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { llap [ i ] = 2 ; } A . sort ( function ( a , b ) { return a - b } ) ; for ( let j = n - 2 ; j >= 0 ; j -- ) { let i = j - 1 ; let k = j + 1 ; while ( i >= 0 && k < n ) { if ( A [ i ] + A [ k ] == ( 2 * A [ j ] ) ) { llap [ j ] = Math . max ( ( llap [ k ] + 1 ) , llap [ j ] ) ; ans = Math . max ( ans , llap [ j ] ) ; i -= 1 ; k += 1 ; } else if ( A [ i ] + A [ k ] < 2 * A [ j ] ) k += 1 ; else i -= 1 ; } } return ans ; } let a = [ 9 , 4 , 7 , 2 , 10 ] ; document . write ( Solution ( a ) + " " ) ;