Getting TLE ? help

#include
#include
using namespace std;
map<int ,int> ans;
int index = 0;
void cal(int n){
if(n==1 || n==0)
{
ans[index] = n;
index++;
return;
}
cal(n/2);
cal(n%2);
cal(n/2);

}
int main()
{

int n,l,r;
cin>>n>>l>>r;

cal(n);
int count_=0;
int i=0;
for(auto a:ans){
if(i>=l and i<=r){
if(a.second == 1){
count_++;
}
}
i++;

}
cout<<count_<<endl;

}

@siddharth9k9You are not including the dp condition that if you have already visited that postiion just return there. You basically need to memoise it.

But this question is if divide and conquer

this is the code that i wrote https://ide.codingblocks.com/s/201265

@siddharth9k9 You need to use dp

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.