**Given an integer N denoting the Length of a line segment. you need to cut the line segment in such a way that the cut length of a line segment each time is integer either x , y or z. and after performing all cutting operation the total number of cutted segments must be maximum. **
#include<bits/stdc++.h>
using namespace std;
int findMaxSegments(int a[],int target,int n,int count)
{
if(n == 0 && target > 0)
return 0;
if(target == 0)
return count;
if(a[n-1] <= target)
return max(findMaxSegments(a,target-a[n-1],n,count+1),findMaxSegments(a,target,n-1,count));
else
return findMaxSegments(a,target,n-1,count);
}
int main()
{
//code
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int queries;
cin>>queries;
while(queries--)
{
int sum;
cin>>sum;
int arr[3];
for(int i=0;i<3;i++)
cin>>arr[i];
int count = findMaxSegments(arr,sum,3,0);
cout<<count<<"\n";
}
return 0;
}
above is recursive solution which is working perfect but taking more time,can anyone help me to change this above code to memoization or top-down dp?
you can check the code for this
INPUT- query->1
N->2683
x,y,z->83 26 2709
OUTPUT 101