Longest Arithmetic Progression | DP | Java program to find Length of the Longest AP ( llap ) in a given sorted set . ; Returns length of the longest AP subset in a given set ; Driver Code
import java . util . * ; class GFG { static int Solution ( int [ ] A ) { int ans = 2 ; int n = A . length ; if ( n <= 2 ) return n ; int [ ] llap = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) llap [ i ] = 2 ; Arrays . sort ( A ) ; for ( int j = n - 2 ; j >= 0 ; j -- ) { int i = j - 1 ; int 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 ; } public static void main ( String [ ] args ) { int [ ] a = { 9 , 4 , 7 , 2 , 10 } ; System . out . print ( Solution ( a ) + "NEW_LINE"); } }