Our professor gave us the following assignment:
A "correct" series is one inwhich the sum of its members equals to the index of its first member.
The program is supposed to find the length of the LONGEST "correct" series within a series of n numbers.
for example: if the input series would be arr[4]={1, 1, 0, 0}
the output (longest "correct" series) would be 3
.
arr[0]=1. 0!=1
therefore the longest series here is 0
.
arr[1]=1,and 1=1.
but the follwing members also sum up to 1 as shown below:
1=arr[1]+arr[2]+arr[3] = 1+ 0 + 0
, therefore the longest series here is 3
.
the output in this example is 3
.
That's what I got so far:
int solve(int arr[], int index, int length,int sum_so_far)
{
int maxwith,maxwithout;
if(index==length)
return 0;
maxwith = 1+ solve(arr,index+1,length,sum_so_far+arr[index]);
maxwithout = solve(arr,index+1,length,arr[index+1]);
if(sum_so_far+arr[index]==index)
if(maxwith>maxwithout)
return maxwith;
return maxwithout;
return 0;
}
int longestIndex(int arr[], int index,int length)
{
return solve(arr,0,length,0);
}
What am I doing wrong here? Thanks a lot for your time...
Harry
oh yeah, we aren't supposed to us eloops on this one. should have mentioned that earlier i guess...