Longest subsequence-1 | Practice | GeeksforGeeks

Given an array A[] of size N, find the longest subsequence such that difference between adjacent elements is one.

Example 1:
Input: N = 7
A[] = {10, 9, 4, 5, 4, 8, 6}
Output: 3
Explaination: The three possible subsequences 
{10, 9, 8} , {4, 5, 4} and {4, 5, 6}.

Example 2:
Input: N = 5
A[] = {1, 2, 3, 4, 5}
Output: 5
Explaination: All the elements can be 
included in the subsequence.

Your Task:
You do not need to read input. Your task is to complete the function longestSubseq() which takes N and A[] as input parameters and returns the length of the longest such subsequence.

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

Constraints:
1 ≤ N ≤ 103
1 ≤ A[i] ≤ 103
0
somyadebsarkar3634 days ago
whats wrong in this??
 
int ans=0;       for(int j=i-1;j>=0;--j){           if(A[i]==A[j]+1 || A[i]==A[j]-1){               ans=max(ans,solve(j,A)+1);       }       return ans;   }          }   int longestSubsequence(int N, int A[])   {          int ans=0;       for(int i=N-1;i>=1;--i){           ans=max(ans,solve(i,A));       }       return ans;       // code here   }
+1
himanshu0719cse192 weeks ago
Number format exception at 3rd test casee??
 
0
milindprajapatmst192 weeks ago
class Solution{
	public:
	unordered_map<int, int> M;
	int longestSubsequence(int n, int arr[]) {  
	    for (int i = 0; i < n; i++) {
	        M[arr[i]] = max(M[arr[i]], 1);
	        for (auto& itr : M) {
	            if (abs(itr.first - arr[i]) == 1) 
	                M[arr[i]] = max(M[arr[i]], itr.second + 1);
	        }
	    }
	    int result = 0;
	    for (auto& itr : M)
	        result = max(result, itr.second);
	    return result;
	}  
};
0
maheshahirwar2043 weeks ago
Java Solution  TC - O(N^2) SC - O(N)
 
 static int longestSubsequence(int N, int A[])   {      int[]dp=new int[N];      int ans=1;      for(int i=0;i<N;i++){          dp[i]=1;          for(int j=0;j<i;j++){              if(Math.abs(A[i]-A[j])==1&&dp[i]<dp[j]+1){                  dp[i]=dp[j]+1;              }          }          ans=Math.max(dp[i],ans);      }      return ans;   }
0
jainmuskan5653 weeks ago
int ls1(int prev,int curr, int A[], int N, vector<vector<int>> &dp){       if(curr==N){           return 0;       }       if(dp[prev+1][curr]!= -1){           return dp[prev+1][curr];       }       int len= 0+ ls1(prev,curr+1,A,N,dp);       if(prev==-1 || abs(A[prev]-A[curr])==1){           len= max(len, 1+ls1(curr,curr+1,A,N,dp));       }       return dp[prev+1][curr]= len;   }   int longestSubsequence(int N, int A[])   {       vector<vector<int>> dp(N+1,vector<int>(N+1,-1));       return ls1(-1,0,A,N,dp);   }};
0
officialshivaji0071 month ago
class Solution{   int ans = INT_MIN;   void solve(int a[],int cur, int n,vector<int> &v){       int m = v.size();       if(cur == n){           ans = max(ans,m);           return;       }              if(m==0 || abs(v[m-1] - a[cur])==1){           v.push_back(a[cur]);           solve(a,cur+1,n,v);           v.pop_back();       }              solve(a,cur+1,n,v);          }public:   int longestSubsequence(int N, int A[])   {       // code here       vector<int> v;       solve(A,0,N,v);       return ans;   }};
0
khushiaggarwal09021 month ago
int dp[1001][1001];   int solve(int n,int prev,int i,int a[])   {            if(n==0||i==n) return 0;                        if(prev==-1)       {           return dp[i][prev]=1+solve(n,i,i+1,a);       }         if(dp[i][prev]!=-1)       return dp[i][prev];                   if(abs(a[i]-a[prev])==1)           {               return dp[i][prev]=max(1+solve(n,i,i+1,a),solve(n,prev,i+1,a));           }           else           {               return dp[i][prev]=solve(n,prev,i+1,a);           }          }
 
   int longestSubsequence(int n, int a[])   {                  memset(dp,-1,sizeof(dp));        return solve(n,-1,0,a);          }
0
harrypotter01 month ago
class Solution:   def longestSubsequence(self, n, a):       dp = [1 for i in range(n+1)]              for i in range(1,n+1):           for j in range(0,i):               if abs(a[i-1]-a[j-1])==1:                   dp[i] = max(dp[i], dp[j]+1)       return max(dp) 
+1
aloksinghbais022 months ago
C++ solution having time complexity as O(N*N) and space complexity as O(N) is as follows :- 
 
Execution Time :- 0.0 / 1.2 sec
 
int longestSubsequence(int n, int a[]){       int dp[n];       for(int i = 0; i < n; i++)           dp[i] = 1;              for(int i = 1; i < n; i++){           for(int j = 0; j < i; j++){               if(abs(a[j]-a[i]) == 1){                   dp[i] = max(dp[i],dp[j] + 1);               }           }       }              return *max_element(dp,dp+n);   }
0
muskankushwah2 months ago
Variant of Longest Increasing Subsequence
int dp[n];       int ans=1;       for(int i=0;i<n;i++){           dp[i]=1;           for(int j=0;j<i;j++){               if(abs(A[i]-A[j])==1&&dp[i]<dp[j]+1)                dp[i]=dp[j]+1;           }           ans=max(dp[i],ans);       }       return ans;
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.