/**/classpublicstaticint Maxlengthpalindrome (int//}
Solution One:
Implemtation is recursive, and it ' s much worse than O (n^2). Should use recursive with memorization and then can improve to O (n^2).
However, recursive memorization doesn ' t improve over bottom-up and it had costly overhead. Bottom-up is better in this problem.
Public classSolution { Public Static voidMain (string[] args) {intArr[] =New int[] {4,1,2,3,4,5,6,5,4,3,4,4,4,4,4,4,4}; System.out.println (arr, maxlengthpalindrome0, Arr.length-1)); } Public Static intMaxlengthpalindrome (int[] values,intIintj) {//Check if indexes cross all other//return 1 if index overlap for else condition below//return 0 if index i<j for condition if below if(j<=i)returnJ-i+1; if(Values[i]==values[j])return2 + maxlengthpalindrome (values, i+1, j-1); Else returnMath.max (Maxlengthpalindrome (values, i+1, J), Maxlengthpalindrome (values, I, j-1)); }}
Solution Two:
The code with the memoization technique which produces O (n^2) complexity are
Public intDplps (int[] A,intIintJ, integer[][] LPs) { if(I >j)return0; if(Lps[i][j] = =NULL) { if(i = =j) Lps[i][j]= 1; Else { if(A[i] = =A[j]) lps[i][j]= 2 + dplps (A, i + 1, j-1, LPs); ElseLps[i][j]= Math.max (Dplps (A, i + 1, J, LPs), Dplps (A, I, J-1, LPs)); } } returnLps[i][j]; }
The function as,
Dplps (A, 0, a.length-1,new integer[a.length][a.length])
*maximum Length palindromic sub-sequence of an Array.