hsnaansh
BAN USER
Comments (3)
Reputation 35
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
2
of 2 vote
O(n^2) complexity solution
public class Solution {
public static void main(String[] args) {
int arr[] = new int[] {4,1,2,3,4,5,6,5,4,3,4,4,4,4,4,4,4};
System.out.println(maxLengthPalindrome(arr, 0, arr.length-1));
}
public static int maxLengthPalindrome(int[] values, int i, int j) {
//check if indexes cross each other
//return 1 if index overlap for else condition below
//return 0 if index i<j for condition if below
if(j<=i) return j-i+1;
if(values[i]==values[j]) return 2 + maxLengthPalindrome(values, i+1, j-1);
else return Math.max(maxLengthPalindrome(values, i+1, j), maxLengthPalindrome(values, i, j-1));
}
}
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
Initialise mem matrix of size*size to -1.
- hsnaansh November 06, 2014