Algorithm in the paper says works only on sorted arrays it is mentioned in the paper itself
On Tue, Jul 12, 2011 at 11:21 PM, Navneet Gupta <navneetn...@gmail.com>wrote: > I wrote the code as someone gave the reference of the paper where algo > to get max arithmetic subsequence was given. > > For an input of {2,9,4,1,6,7,8,3,10}, i am getting an output of 3, > while it should be 5 for {2,4,6,8,10} > > Below is the implementation, can someone help me understand where am i > going wrong? > > int LongestArithmeticSubsequence(int *a, int len) > { > int maxLen = 2;int i,k; > int **L = (int **)new int[len]; > for(int m = 0; m <len; m++) > L[m] = new int[len]; > for(int j = len - 1; j>=1;j--) > { > i = j-1; > k = j+1; > while(i>=1&&k<=len-1) > { > if(a[i]+a[k]<2*a[j]) > k++; > else if(a[i]+a[k]>2*a[j]) > { > L[i][j] = 2; > i = i-1; > } > else > { > L[i][j] = L[j][k] + 1; > maxLen = getMax(maxLen, L[i][j]); > i = i-1; k = k+1; > } > } > while(i>=1) > { > L[i][j] = 2; > i=i-1; > } > } > return maxLen; > } > > -- > Regards, > Navneet > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > > -- Sunny Aggrawal B-Tech IV year,CSI Indian Institute Of Technology,Roorkee -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.