#include<iostream> using namespace std;
int maxsum(int a[], int n) { int prevmaxsum=0; int currmaxsum=a[0]; int last=0,temp=0; for(int i=1;i<n;i++) { if(last!=(i-1)) { prevmaxsum=currmaxsum; currmaxsum=a[i]+prevmaxsum; last=i; cout<<prevmaxsum<<"\t\t\t"<<currmaxsum<<"\t\t\t"<<last<<endl; continue; } if((a[i]+prevmaxsum)>currmaxsum) { temp=a[i]+prevmaxsum; prevmaxsum=currmaxsum; currmaxsum=temp; last=i; cout<<prevmaxsum<<"\t\t\t"<<currmaxsum<<"\t\t\t"<<last<<endl; } } return currmaxsum; } int main() { int arr[6]={3, 2 ,5,7 ,10}; int num=5; int sum=maxsum(arr,6); cout<<sum; getchar(); return 0; } On Fri, Oct 9, 2009 at 9:26 AM, ankur aggarwal <ankur.mast....@gmail.com>wrote: > > 2. Given an array all of whose elements are positive numbers, find the > maximum sum of a subsequence with the constraint that no 2 numbers in the > sequence should be adjacent in the array. > > i) 3 2 7 10 should return 13 (sum of 3 and 10) > > ii) 3 2 5 10 7 should return 15 (sum of 3, 5 and 7) > > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---