Ferris wheel CSES problem

https://cses.fi/problemset/task/1090/

What I am trying to do.

sort the weights in increasing order.
keep on counting the number of gondolas required as the array is traversed. If the gondola can’t hold a person, update the gondola weight, and increase the gondola count.

Still, the correct answer is not coming, I tried many ways to debug my code but couldn’t find what is wrong in my approach.
Pls, help me.

Here is my approach
Sort the array
Apply two pointer

#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,x;
cin>>n>>x;
int a[n],i,j;
for(i=0;i<n;i++)
{
cin>>a[i];
}
sort(a,a+n);
int ans=0;
i=0,j=n-1;
while(i<=j)
{
if(a[i]+a[j]<=x)
{
ans++;
i++;
j–;
}
else
{
j–;
ans++;
}
}
cout<<ans<<endl;
}

1 Like

Thanks for the solution !. I know there is a 2 pointer approach, but how will I learn if don’t find what is wrong in my solution. Pls, help.

Ok I’ll give u a testcase

4 10
1 5 5 9

your o/p = 3
original o/p = 2 ((1,9) , (5,5))

Hope u got it.

got it thanks !. So the intuition behind this approach was to put 1 small and 1 big guy into a gandola, i mean pick form 2 extremes if we can, that way min gandolas could be used ?

yes you got it now