1339C. Powered Addition - Codeforces Solution C++

  Problem Link : 1339C. Powered Addition 


✅ C++ Solution :

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

#define ll long long int
#define pb push_back
#define mp make_pair

ll m =1000000007;

bool func(ll t, ll arr[],ll n)
{
	ll v[n];
	v[0]=arr[0];
	ll d;
	ll val=pow(2,t)-1;
	//cout<<t<<" "<<val<<endl;
	for(int i=1;i<n;i++)
	{
		if(arr[i]>=v[i-1])
			v[i]=arr[i];
		else
		{
			d=v[i-1]-arr[i];
			if(d<=val)
				v[i]=v[i-1];
			else
				return 0;
		}
	}
	return 1;



}

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
	    ll n;
		cin>>n;
		ll arr[n];
		for(int i=0;i<n;i++)
			cin>>arr[i];

		ll low=0,mid,high=pow(10,2)+1;


		while(low<high)
		{

			mid=low+(high-low)/2;
			if(func(mid,arr,n))
				high=mid;
			else
				low=mid+1;
		}

		cout<<low<<"\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!!