Longest Arithmetic Progression | Practice | GeeksforGeeks

Given an array called A[] of sorted integers having no duplicates, find the length of the Longest Arithmetic Progression (LLAP) in it.

Example 1:
Input:
N = 6
set[] = {1, 7, 10, 13, 14, 19}
Output: 4
Explanation: The longest arithmetic 
progression is {1, 7, 13, 19}.

Example 2:
Input:
N = 5
A[] = {2, 4, 6, 8, 10}
Output: 5
Explanation: The whole set is in AP.

Your Task:
You don't need to read input or print anything. Your task is to complete the function lenghtOfLongestAP() which takes the array of integers called set[] and n as input parameters and returns the length of LLAP.

Expected Time Complexity: O(N2)
Expected Auxiliary Space: O(N2)

Constraints:
1 ≤ N ≤ 1000
1 ≤ set[i] ≤ 104
0
paawansingal3 days ago
int lengthOfLongestAP(int a[], int n) {       // code here              if(n == 1)           return 1;              vector<vector<int >> dp(n + 1, vector<int>(1e4 + 3, 1));       int ans = 0;       for(int i = 1; i <= n; ++i){           for(int j = i + 1; j <= n; ++j){                              int df = a[j - 1] - a[i - 1];               dp[j][df] = 1 + dp[i][df];               ans = max(ans, dp[j][df]);           }       }              return ans;          }
0
ksridharan8295 days ago
class Solution{   
public:
   int lengthOfLongestAP(int A[], int n) {
   
    vector<vector<int>> dp(1001 , vector<int>(10005 , 0));
    int mx = 0;
    for (int i = 1 ; i < n ; i++){
        for (int j = 0 ; j<i ; j++){
           dp[i][A[i]-A[j]] = max(dp[i][A[i]-A[j]] , 1 + dp[j][A[i]-A[j]]);
           mx = max(dp[i][A[i]-A[j]]  , mx);
        }
    }
    return mx+1;
   }
};
0
imranwahid2 weeks ago
Easy C++ solution
0
geminicode1 month ago
why are we putting set[i][j] = set[j][k] + 1??? can anyone explain plz..
0
mocambo1 month ago
simplest one
 int lengthOfLongestAP(int A[], int n) {
     //using map to serach element;
     unordered_map<int ,int> mep;
     for(int i=0; i<n; i++){
         mep[A[i]]++;
     }
     
     int ans=1;//for corner case:when n=1
     
     for(int i=0; i<n-1; i++){
         int count;
         for(int j=i+1; j<n; j++){
             int cd=A[j]-A[i];//common diffrence
             count=2;
             int curr=A[j];
             /*serching for succesive element of 	   progression in the map with current cd*/
             while(mep[cd+curr]!=0){
                 count++;
                 curr+=cd;
             }
             ans=max(ans,count);
         }
     }
     return ans;
    }
+1
ruchitchudasama1232 months ago
public:
   int lengthOfLongestAP(int A[], int n) {
       if(n<=2) return n;
       int maxLen=2;
       vector<unordered_map<int,int>> dp(n);
       
       for(int i=0;i<n;i++){
           for(int j=i+1;j<n;j++){
               int diff=A[j]-A[i];
               if(dp[i].find(diff)!=dp[i].end()){
                   dp[j][diff]=dp[i][diff]+1;
               }
               else{
                   dp[j][diff]=2;
               }
               maxLen=max(maxLen,dp[j][diff]);
           }
       }
       return maxLen;
   }
0
ezio3 months ago
class Solution {
    int lengthOfLongestAP(int[] nums, int n) {
        Map<Integer, Integer>[] dp = new HashMap[n];
        for (int i = 0; i < n; i++)
            dp[i] = new HashMap<>();

        int maxLen = 1;
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < i; j++) {
                int cd = nums[i] - nums[j];
                if (dp[j].containsKey(cd)) {
                    dp[i].put(cd, dp[j].get(cd) + 1);
                } else {
                    dp[i].put(cd, 2);
                }
                maxLen = Math.max(maxLen, dp[i].get(cd));
            }
        }
        return maxLen;
    }
}

0
ezio
This comment was deleted.
+2
harshitrepa5 months ago
        if(n==1) return 1;              if(n==2) return 2;              vector<unordered_map<int,int>> dp(n);       int ans = 0;       for(int i=1;i<n;i++)       {                      for(int j=0;j<i;j++)           {               int diff = arr[i] - arr[j];                              if(dp[j].find(diff)==dp[j].end())               {                   dp[i][diff] = 2;               }else               {                   dp[i][diff] = dp[j][diff]+1;               }               ans = max(ans,dp[i][diff]);                          }       }             return ans;
0
siddhantp619
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.