Cratylus
Cratylus

Reputation: 54074

Java confusion on fundamentals on double and integer and conversions

I am practicing in an automatic judge for algorithm in one of the carreer sites and I have the following question.
In one of the puzzles the question is to find the median of 2 sorted arrays and the signature of the method to implement is:

public double findMedianSortedArrays(int A[], int B[])  

I written the code and some of the tests failed.
Looking at the results though the failures reported are as follows:

Input       Output  Expected    
[], [1]      1.0    1    
[2], []      2.0    2  

It seems I am rusty on the fundamentals.

My question: How can it expect an 1 or 2 and reject 1.0 if the method returns a double?
My snippet of code on the calculation of merge is:

 if(array.length %2 == 0){

     return (array[middle] + array[middle - 1])/(double)2;    
 }
 else{
      return (double)array[middle];

  }

enter image description here

Upvotes: 1

Views: 134

Answers (1)

The Nail
The Nail

Reputation: 8490

It's an error in the judge. If I go to the site, select 'Java' and fill in the following Java implementation (just filling in one line, the return 0;:

public class Solution {
    public double findMedianSortedArrays(int A[], int B[]) {
        // Start typing your Java solution below
        // DO NOT write main() function
        return 0;
    }
}

the report states output: 0.0 for all cases.

The C++ equivalent does not seem to suffer from the same problem. If I fill in this:

class Solution {
public:
    double findMedianSortedArrays(int A[], int m, int B[], int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        return 0;
    }
};

then the report states output: 0 for all cases.

Upvotes: 2

Related Questions