Can just give hint on how to write recursive for this
Cn some1 help me in recursion of this?
Hey @pranjalarora98
int vivekGame(int *arr,int *pre,int s,int e){ //arr is array , pre is prefix sum , s & e are starting ,ending index
//base cases
if (s>=e){ //he cant divide return 0
return 0;
}
if ((s+1)==e){ //only 2 elements left base case
if (arr[s]==arr[e]){
return 1;
}else{
return 0;
}
}
int smallans=0; //to hold points
for (int i=s;i<e;i++){
if ((pre[i+1]-pre[s])==(pre[e+1]-pre[i+1])){ //sum of first half same as 2nd half
smallans =(max(vivekGame(arr,pre,s,i),vivekGame(arr,pre,i+1,e)))+1; //divide and recurse
// Since only one break point possible break after that point is founnd
break;
}
}
return smallans;
}
And 1 thing when i am submitting same prob on hackerrank getting tle in some cases.How to optimise further?
Hey @pranjalarora98
I am not sure if this can be optimized further
Refer to hackerrank editorial if there is any optimized approach