the code link
where i am wrong please check the code
instead of ans=max(mid,ans);
just assign ans = mid