i am not able to understand why it is giving compilation error
Tuglaq and Copper Coins
Working fine but why we r not able to use
#define max 1e9
int dp[max]={0};
why it is wrong?
this is because 1e9 is very large for array size.
though i am using 1e5
source.cpp: In function ‘int32_t main()’:
source.cpp:4:13: error: conversion from ‘double’ to ‘long unsigned int’ in a converted constant expression
4 | #define MAX 1e5
| ^~~
source.cpp:26:12: note: in expansion of macro ‘MAX’
26 | int dp[MAX]={0};
| ^~~
source.cpp:4:13: error: could not convert ‘1.0e+5’ from ‘double’ to ‘long unsigned int’
4 | #define MAX 1e5
| ^~~
source.cpp:26:12: note: in expansion of macro ‘MAX’
26 | int dp[MAX]={0};
| ^~~
source.cpp:4:13: error: size of array ‘dp’ has non-integral type ‘double’
4 | #define MAX 1e5
| ^~~
source.cpp:26:12: note: in expansion of macro ‘MAX’
26 | int dp[MAX]={0};
| ^~~
this is the error i am facing
it is just a good practice to initalise with the way i have done to avoid these conversion errors.
ok but my code is giving me RunTime error…Why? i have applied dp
@dare_devil_007 i was wondering why you havent put this question as a doubt .
letme explain you this is because that there are cases for n if n is less than 10 then simply return n.
Here for your reference i am attaching the correct code :
Sorry i did not quite understood your code.Could you please explain me in detail
i have just added one case i.e if n is less than 10 then you simply have to return n because if you dry run the code for n less than 10 then rather then its breakage the number originally will be maximum.
that is why we have added this case.
there are just if else statement .
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define MAX 1e9 + 7
int getGold(int n,int dp[]){
if(n<10){
return n;
}
if(dp[n]!=0){
return dp[n];
}
int op1 = getGold(n/2,dp);
int op2 = getGold(n/3,dp);
int op3 = getGold(n/4,dp);
return dp[n] =max(n,(op1+op2+op3));
}
int32_t main(){
int n;
cin >> n;
int dp[1000000]={0};
cout<<getGold(n,dp)<<endl;
}
This Code still giving me RE (Explain me Why …)that is why i was asking you whether if else condition of your code mean something or not
analyse your code correctly .you are not making the use of DP array .
if the array have result for particular n then you have to return dp[n].
please dry run your code for as many times as you can .
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define MAX 1e9 + 7
int getGold(int n,int dp[]){
if(n<10){
return dp[n]=n;//calculating and adding it to dp array
}
//Here iam using my dp array,when there is a n which is calculated i am returning it
if(dp[n]!=0){
return dp[n];
}
int op1 = getGold(n/2,dp);
int op2 = getGold(n/3,dp);
int op3 = getGold(n/4,dp);
return dp[n] =max(n,(op1+op2+op3));
}
int32_t main(){
int n;
cin >> n;
int dp[1000000]={0};
cout<<getGold(n,dp)<<endl;
}
but while submitting it is giving me RE
@dare_devil_007 then you have to make cases as i had made in my code which i have also shared with you .
Happy Learning !!
Bhai ye btana ki mai kaha galat kr rha hu taaki mai kuch seekh saku…plz bro point out mistakes in my code where can my code go wrong
@dare_devil_007 if you have seen in my code i have made cases for n<10 and n<1e6 and else of n>1e6.
this is becasue as n can be 1e9 also but we cannot make an array of size 1e9 that why i had made the cases.
because we cannot make the array of size 1e9 .
this is the problem that is why it is showing run error .
please read the question many times in case you dont understand .
i was saying the same thing.
for some questions there are some tricks .
i hope i have cleared your doubt .
Happy Learning !!
Thanks a lot man!!! i did’nt knew that we can’t take array more than 1e6 size,that is why i was struggling.
i have learnt a new thing because of you.
Once again thanks a lot!1