Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int dp[1005][1005];
- int lengthOfLongestAP(int A[], int n) {
- int res = 2;
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < n; j++) {
- dp[i][j] = 2;
- }
- }
- for(int j = n - 2; j > 0; j--) {
- int i = j - 1;
- int k = j + 1;
- while(i >= 0 and k < n) {
- if(A[i] + A[k] == 2 * A[j]) {
- dp[i][j] = dp[j][k] + 1;
- i--;
- k++;
- }
- else if(A[i] + A[k] < 2 * A[j]) {
- k++;
- }
- else {
- i--;
- }
- }
- }
- for(int i= 0; i < n; i++) {
- for(int j = 0; j < n; j++) {
- res = max(res, dp[i][j]);
- }
- }
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement