Help in binary search problem

Revision en3, by colorBlindCoder, 2022-10-30 01:26:55

I am trying to solve this problem, facing probably an overflow issue.

Here's my code:

#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define NL cout<<"\n";

typedef long long int lli;

void Solve(){
	lli w, h, n; cin >> w >> h >> n;
	lli l=1, r = (sqrt(n)+1)*(max(w,h)), mid;
	while (l + 1 < r) {
		mid = l + (r-l)/2;
		if ((mid/w) *(mid/h) > n) {
			r = mid;
		} else {
			l = mid;
		}
	}
	// cout << l << ", " << r;NL
	if ((l/w)*(l/h) == n) {
		cout << l;NL;
	} else {
		cout << r;NL;
	}
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	lli t;
	// cin >> t;
	t=1;
	for (int i=0; i<t; ++i) { Solve();}
	return 0;
}

I am sure there's an overflow issue but I have one more doubt. After being unsuccessful after many attempts, I started reading comments under the problems and found a guy who posted his working solution as:

#include<bits/stdc++.h>
#define ll long long

using namespace std;
int main(){
	unsigned ll a, b, n;
	cin >> a >> b >> n;
	unsigned ll l = 0, r = 1e18;

	while(r > l+1){

		unsigned ll m = (l+r)/2;
		if((m/a)*(m/b) >= n){
			r = m;
		}
		else l = m;
	}
	cout << r;
}

I read his code and found he didn't do anything for the overflow issue except use an unsigned data type. I submitted his code just to see if it works but it failed in the $$$27^{th}$$$ test case (Mine was failing in the $$$5^{th}$$$ test case). So I thought I should try my code with unsigned long long, and therefore I did, here's the code:

#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define NL cout<<"\n";

#define ll long long

void Solve(){
	unsigned ll w, h, n; cin >> w >> h >> n;
	unsigned ll l=0, r = 1e18, mid;
	while (l + 1 < r) {
		mid = l + (r-l)/2;
		if ((mid/w) *(mid/h) > n) {
			r = mid;
		} else {
			l = mid;
		}
	}
	// cout << l << ", " << r;NL
	if ((l/w)*(l/h) >= n) {
		cout << l;NL;
	} else {
		cout << r;NL;
	}
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	ll t;
	// cin >> t;
	t=1;
	for (int i=0; i<t; ++i) { Solve();}
	return 0;
}

But still my code failed on $$$5^{th}$$$ test case only. That makes me doubt my binary search algorithm.

I have tried to maintain the following invariants: $$$f(l) <= n$$$, and $$$f(r) > n$$$, throughout the binary search loop, and then at the end I check if $$$f(l) == n$$$ or not if yes then return $$$l$$$ otherwise $$$r$$$.

Please have a look at my binary search algo and see if it is correct. Also, it would be great if you give me some hint on how to avoid overflow.

Thanks a lot! May you have a wonderful day!

Tags binary search, need help, int overflow

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en7 English colorBlindCoder 2022-10-30 01:35:11 25
en6 English colorBlindCoder 2022-10-30 01:33:29 0 (published)
en5 English colorBlindCoder 2022-10-30 01:33:03 209
en4 English colorBlindCoder 2022-10-30 01:28:33 154
en3 English colorBlindCoder 2022-10-30 01:26:55 46 Tiny change: 'ariants:\n **1.** f(' -> 'ariants:\n**1.** f('
en2 English colorBlindCoder 2022-10-30 01:20:51 11
en1 English colorBlindCoder 2022-10-30 01:18:52 2816 Initial revision (saved to drafts)