279B. Books - Codeforces Solution C++

  Problem Link : 279B. Books 


✅ C++ Solution :

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

#define ll long long int

bool func(ll v,ll cum[],int n,ll t)
{
	ll val;
	for(int i=0;i+v<=n;i++)
	{
		if(i==0)
			val=cum[i+v-1];
		else
			val=cum[i+v-1]-cum[i-1];

		if(val<=t)
			return true;
	}
	return false;
}

int main()
{
	int n;
	ll t;
	cin>>n>>t;
	int arr[n];
	ll cum[n];
	for(int i=0;i<n;i++)
	{
		cin>>arr[i];
		if(i!=0)
			cum[i]=cum[i-1]+arr[i];
		else
			cum[i]=arr[i];
	}

	ll low=0,high=n,mid;
	while(low<high)
	{
		mid=low+(high-low+1)/2;
		if(func(mid,cum,n,t))
			low=mid;
		else
			high=mid-1;
	}
	cout<<high;
}

 

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!!