Let's Begin- HackerEarth- Dynamic Programming- Easy
Solution:-
using namespace std;
#define MAX 1000010
int arr[MAX];
void cal()
{
for(int i=0;i<MAX;i++)
arr[i]=INT_MAX;
arr[2]=arr[3]=arr[5]=arr[7]=1;
int a;
for(int i=4;i<=MAX;i++)
{
a=9999999;
if(arr[i-2])
a=arr[i-2];
if(arr[i-3])
a=min(a,arr[i-3]);
if(i-5>=0 && arr[i-5])
a=min(a,arr[i-5]);
if(i-7>=0 && arr[i-7])
a=min(a,arr[i-7]);
arr[i]=min(1+a,arr[i]);
}
}
int main()
{
cal();
int tc;
cin>>tc;
while(tc--)
{
int N;
cin>>N;
if(arr[N]<INT_MAX)
cout<<arr[N];
else
cout<<"-1";
cout<<endl;
}
return 0;
}
No comments:
Post a Comment