Skip to content

Commit f92406a

Browse files
authored
Create Q-08: Longest Arithmetic Subsequence.java
1 parent a9788b1 commit f92406a

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
1027. Longest Arithmetic Subsequence
3+
4+
Given an array nums of integers, return the length of the longest arithmetic subsequence in nums.
5+
Note that:
6+
A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
7+
A sequence seq is arithmetic if seq[i + 1] - seq[i] are all the same value (for 0 <= i < seq.length - 1).
8+
9+
Example 1:
10+
Input: nums = [3,6,9,12]
11+
Output: 4
12+
Explanation: The whole array is an arithmetic sequence with steps of length = 3.
13+
14+
Example 2:
15+
Input: nums = [9,4,7,2,10]
16+
Output: 3
17+
Explanation: The longest arithmetic subsequence is [4,7,10].
18+
19+
Example 3:
20+
Input: nums = [20,1,15,3,10,5,8]
21+
Output: 4
22+
Explanation: The longest arithmetic subsequence is [20,15,10,5].
23+
24+
25+
Constraints:
26+
27+
2 <= nums.length <= 1000
28+
0 <= nums[i] <= 500
29+
*/
30+
class Solution {
31+
static HashMap<String,Integer> map;
32+
public int longestArithSeqLength(int[] nums) {
33+
// Recursion::1.
34+
//return solveRec(nums,0,501,-1,0);
35+
36+
// Recursion Modiied::2
37+
// return solve(Integer.MIN_VALUE,Integer.MIN_VALUE,nums,0);
38+
39+
// map =new HashMap<>();
40+
// return solveMemo(Integer.MIN_VALUE,Integer.MIN_VALUE,nums,0);
41+
return solveTab(nums);
42+
43+
}
44+
public static int solveRec(int[] nums,int indx,int diff,int prev,int len){
45+
//base case:::
46+
if(indx>=nums.length) return 0;
47+
if(diff==501){
48+
int pick=0;
49+
if(len<1){
50+
pick =1+solveRec(nums,indx+1,diff,indx,len+1);
51+
}else {
52+
pick =1+solveRec(nums,indx+1,nums[indx]-nums[prev],indx,len+1);
53+
}
54+
int notpick =solveRec(nums,indx+1,diff,prev,len);
55+
return Math.max(pick,notpick);
56+
}else{
57+
int pick=0;
58+
if(nums[indx]-nums[prev]==diff){
59+
pick =1+solveRec(nums,indx+1,diff,indx,len+1);
60+
}
61+
int notpick =solveRec(nums,indx+1,diff,prev,len);
62+
return Math.max(pick,notpick);
63+
}
64+
}
65+
public static int solve(int prev,int diff,int[] nums,int indx){
66+
if(indx==nums.length) return 0;
67+
int pick=0;
68+
int notpick=0;
69+
70+
if(prev==Integer.MIN_VALUE){
71+
pick =1+solve(nums[indx],diff,nums,indx+1);
72+
}else if(diff==Integer.MIN_VALUE || diff == nums[indx]-prev){
73+
pick=1+solve(nums[indx],nums[indx]-prev,nums,indx+1);
74+
}
75+
notpick =solve(prev,diff,nums,indx+1);
76+
return Math.max(pick,notpick);
77+
}
78+
public static int solveMemo(int prev,int diff,int[] nums,int indx){
79+
if(indx==nums.length) return 0;
80+
81+
String key =indx+"-"+prev+"-"+diff;
82+
if(map.containsKey(key)){
83+
return map.get(key);
84+
}
85+
int pick=0;
86+
int notpick=0;
87+
88+
if(prev==Integer.MIN_VALUE){
89+
pick =1+solveMemo(nums[indx],diff,nums,indx+1);
90+
}else if(diff==Integer.MIN_VALUE || diff == nums[indx]-prev){
91+
pick=1+solveMemo(nums[indx],nums[indx]-prev,nums,indx+1);
92+
}
93+
notpick =solveMemo(prev,diff,nums,indx+1);
94+
map.put(key,Math.max(pick,notpick));
95+
return Math.max(pick,notpick);
96+
}
97+
public static int solveTab(int[] nums){
98+
int n=nums.length;
99+
100+
if(n<=2) return n;
101+
Map<Integer, Integer>[] dp = new HashMap[n];
102+
int longest=2;
103+
for(int i=0;i<n;i++){
104+
dp[i] =new HashMap<>();
105+
for(int j=0;j<i;j++){
106+
int diff =nums[i]-nums[j];
107+
dp[i].put(diff,dp[j].getOrDefault(diff,1)+1);
108+
longest =Math.max(longest,dp[i].get(diff));
109+
}
110+
}
111+
return longest;
112+
}
113+
114+
}

0 commit comments

Comments
 (0)