Hotels Along the Croatian Coast
Link to the question : HOTELS
HINT :
We apply the sliding concept. We start calculating the sum from index 0 of the array. If the sum exceeds the maximum amount given, we start subtracting from sum from the beginning of the array, untill the sum becomes less than or equal to maximum amount.
SOURCE CODE :
#include<stdio.h>
int main()
{
int i, l;
long long n, m, sum, max_sum;
scanf("%lld%lld",&n, &m);
int a[n];
for(i=1; i<=n; i++)
{
scanf("%d", &a[i]);
}
l=1;
max_sum = 0;
sum = 0;
for(i=1; i<=n; i++)
{
sum = sum + a[i];
while(sum>m)
{
sum = sum - a[l];
l++;
if(max_sum<=sum && sum<=m)
{
max_sum = sum;
break;
}
}
if(max_sum<=sum && sum<=m)
{
max_sum = sum;
}
}
printf("%lld\n", max_sum);
return 0;
}
Is there a need to break? while will itself end when sum<=m
ReplyDeleteNo. I noticed it now :p
DeleteYou have a bug in your implementation. You start calculating the sum from index 1 whereas arrays in C begin at index 0.
ReplyDeleteYes, arrays begin with 0. But here I am not using the 0th index.
Delete#include
ReplyDeleteusing namespace std;
int main(){
long long int n,m,max=0;
cin>>n>>m;
long long int a[n];
long long int total=0,temp;
long long int tot=0;
long long int counter =0;
for(long long int i=0;i>temp;
tot+=temp;
a[i]=temp;
total+=temp;
while(total>m){
l:if(totaltot){
cout<<tot;
}else{
cout<<max<<"\n";
}
}
Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking.
ReplyDeleteapartamentos cerca de la playa
I just thought it may be an idea to post incase anyone else was having problems researching but I am a little unsure if I am allowed to put names and addresses on here.
ReplyDeletereservations.com
I am very much pleased with the contents you have mentioned. I wanted to thank you for this great article.
ReplyDeleteHotels in Bournemouth
I am truly inspired by this online journal! Extremely clear clarification of issues is given and it is open to every living soul. I have perused your post, truly you have given this extraordinary informative data about it.Dubrovnik Accommodation
ReplyDeleteGraceful written content on this blog is really useful for everyone same as I got to know. Difficult to locate relevant and useful informative blog as I found this one to get more knowledge but this is really a nice one.2 stars hotels
ReplyDeleteBlackjack 101 Casino - Mapyro
ReplyDeleteBlackjack 101 나주 출장샵 Casino. Casinos 동두천 출장안마 in San Francisco. Information on gambling in San 이천 출장마사지 Francisco, California, in the Valley, California 청주 출장안마 and in other 수원 출장마사지 places to go