Starters28,Div3,Problem:CarChoice,Either WrongQuestionOrWrongAnswer By CodeChef

This is not fair CodeChef. You had clearly written integers in the question. So why are you accepting solutions in which it is taken float. Just because of this, I could not submit a correct answer, which actually is the correct answer.

the solution which is actually the correct answer, but given wrong by CodeChef
the solution which is actually the wrong answer, but given correct by CodeChef

It can clearly be seen in the solutions, the only difference is the data type.
And it can also be seen that CodeChef clearly specified to use integer data type:

Why do you think your implementation is correct?

If using int would not pass, then why was my submission accepted?

My Solution using Integers
void solve() {
	int x1 = 0, x2 = 0, y1 = 0, y2 = 0;
	cin >> x1 >> y1 >> x2 >> y2;
	if(x1 * y2 > x2 * y1) {
		cout << "-1" << '\n';
	}
	else if(x1 * y2 < x2 * y1) {
		cout << "1" << '\n';
	}
	else {
		cout << 0 << '\n';
	}
}

FYI: Arithmetic operators - cppreference.com

If you divide two integers the result will always be an integer. if you want float you have to do implicit casting
Example:

int a = 8;
int b = 5;
float c = a/b;

Here even though c is float, the value in c will be 1 not 1.6

int a = 8;
int b = 5;
float c = (float) a/b;

Here the value of c will be 1.6

If you don’t want to do casting you can take both values as a float then on dividing the resultant value will also be float

float a = 8;
float b = 5;
float c = a/b;

Here the value of c will be 1.6

Then what is the reason that my solution didn’t get accepted? :slightly_smiling_face:

I could not understand how is this information related to what I am saying !?

  • The solution is based on the result of (x1 / y1) and (x2 / y2).

    • If (x1 / y1) < (x2 / y2), print "1".
    • If (x1 / y1) > (x2 / y2), print "-1".
    • If (x1 / y1) = (x2 / y2), print "0".
  • Now, what could go wrong in evaluating (x1 / y1) and (x2 / y2)? (Consider all of them as 32-bit signed integers)

    • The result of (x1 / y1) is going to be an integer (Try executing the Demo code)
    • So, the result of (x1 / y1) will not be correct unless y1 divides x1, and the input doesn’t guarantee y1 divides x1 (neither y2 divides x2).
    • Hence, the comparison will be made on truncated results which is not what we want.
Integer division demo
#include <bits/stdc++.h>
using namespace std;

int main() {
    int a = 15, b = 10;
    cout << (a / b) << '\n';
    // don't expect the result to be 1.5
    return 0;
}
Try this Sample against your code

Input

1
14 13 10 9

Expected Output

1

Your Output

0
  • How to handle then?

    1. Use float (or double if high precision is needed). If one or both of (x1, y1) are float, then the result (x1 / y1) will be float. There will not be any truncation and you can do the comparison without any problem.

    2. Do some math and re-arrange the terms:

      • Comparing (x1 / y1) and (x2 / y2) can also be done by re-arranging.
      • The result of (x1 / y1) < (x2 / y2) will be same as that of (x1 * y2) < (x2 * y1). (Make sure they are positive).
      • So, comparing (x1 / y1) and (x2 / y2) can be erroneous, while comparing (x1 * y2) and (x2 * y1) will not be.