Winnin CB Scholorship: Test case failing

I have applied logic as suggested in the video.
Also, I have kept int mind about the long long int.
Still 3 out of 6 test cases are failing. Please check my logic.
link : https://ide.geeksforgeeks.org/kQWSWZMrzZ

#include
using namespace std;

int main() {
long N,M,X,Y;
cin>>N>>M>>X>>Y;
long SC=M/X; //students with coupons
long SW=N-SC; //students without coupons
long CL=M-SCX; //coupons left
while(SW>1){ /
check if there is no more student left*/
CL+=Y;
–SW;
while(CL>=X){ /charges coupons from weak students and assign it to deserving/
CL-=X;
++SC;
–SW;
If(SW==0) //checks is no more student left
break;
}
}
if(SC>=N)
cout<<N;
else
cout<<SC;
return 0;
}

Check if your code is checking for all the conditions by comparing it with my code.

Hope this would help.
If you still have doubts, feel free to ask.

If your doubt is solved. Please, mark it as resolved and if possible give a like.

Well,I just wanted know what was wrong in my code. The logic used in your code is different. So It’s difficult to compare yours with mine. Please help me with my code sir. That would be a great help.

I didn’t want you to compare the syntax or logic. I wanted you to check whether your code is satisfying all the conditions or not. I’ll review your code and let you know the mistakes.

Thanks.Please let me know my mistakes so that I can rectify it.

the logic of your code is correct. But it is failing for large values of variables.

The problem is with the multiplication:
long long int brilliant= mid * x;

Here both the operands x and mid are of integer data type.

If you multiply two integer values, the result will be an integer value. Then, the assignment of integer value to long long is meaningless.

But the actual result of multiplication is coming out in a range greater than that of integer.
So this code puts wrong value into “brilliant”(because here you have integer overflow).

Solution:
bool isValid(int n, int m, int x, int y, long long int mid.){}

As we have changed the type of mid to long long int, so now the result will be of long long int type. Hence, integer overflow his been handled.

Hope, this would help.

Great explanation sir. Thanks a lot :slight_smile:

Mark it as responded and i won’t mind if you’ll give a like.:wink: