1166C. A Tale of Two Lands - Codeforces Solution C++

  Problem Link : 1166C. A Tale of Two Lands 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;

#define ll long long int

int main()
{
	int n;
	cin>>n;
	ll arr[n];
	for(int i=0;i<n;i++)
	{
		cin>>arr[i];
		arr[i]=abs(arr[i]);
	}

	sort(arr,arr+n);

	ll ans=0;
	ll low,mid,high;
	for(int i=0;i<n-1;i++)
	{
		low=i;
		high = n-1;

		while(low<high)
		{
			mid=low+(high-low+1)/2;
			if(arr[mid]<=2*arr[i])
				low=mid;
			else
				high=mid-1;
		}
		ans+=high-i;

	}

	cout<<ans<<"\n";

}

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!