Job for Bounties recursive cases

I have been trying this problem, i came up a solution but it seems to work on only the same input given. Below is my recursive approach

prinmax(arr,0,1,n,0)

public static int prinmax(char arr[],int beg,int end,int n,int k){
if(end==n)
return 0;

    if(beg>=end)
        return 0;

    if(k==0) {
        if(s==1)
        return 1;
        s=1;
    }

    if(arr[beg]=='(')
        k++;
    if(arr[beg]==')')
        k--;
    int ans=0;

    ans=Math.max(ans,prinmax(arr,beg,end+1,n,k)+prinmax(arr,beg+1,end,n,k));

    return ans;
}

please tell me the correct recursive approach for this problem

stack<pair<ll,ll>> S;
S.push({(s[0]==’(’?0:1),0});
ll dp[n+1]={};
for(i=1;i<n;i++){
if(s[i]==’)’){
cnt=0;
if(!S.empty()){
if(S.top().F==0){
dp[S.top().S]=2;
dp[i]=2;
S.pop();
}
else{
S.push({1,i});
}
}
else{
S.push({1,i});
}
}
else{
S.push({0,i});
}
}
try to understand this approach ,

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.