how do i to convert recursion code to for/while loop

Revision en1, by yogomate, 2023-02-02 14:08:05

here i have a recursion code specifically a binary search code(recurion) ll bs(ll l, ll h, vector wins[], ll k, ll i)// { if(l<=h) { ll m=(l+h)/2;

if(wins[i][m]<=k){
        if(m+1<=h){
            if(wins[i][m+1]<=k){
                return bs(m+1, h, wins, k, i);
            }else{
                return m+1;
            }
        }else{
            return m+1;
        }
    }else{
        return bs(l, m-1, wins, k, i);
    }
}else{
    return 0;
}

} and i converted it to while loop binary search but i am not getting desired output my code : ll binary(vector wincnt, ll player, vector rounds[], ll k){ int ans = 0; ll l = 0; ll r = wincnt[player]-1; while(l <= r){ int mid = l + (r — l) / 2; if(mid+1<=r){ if(rounds[player][mid+1]<=k){ l = mid+1; }else{ return m+1; } } else{ r = mid-1; } } return ans; } what did i do wrong

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English yogomate 2023-02-02 14:08:05 1208 Initial revision (published)