I have seen that this question has been asked earlier, this is the link : Apple and Orange HackerRank. I must tell you that my doubt is not like this person, it is just that I've written a code which works fine on for most of the test cases, and fails for some either.
I have checked my code, and I'm fairly confident that my code is something which is working fine.
The question link is : Apple And Orange HackerRank Question
Problem Statement
Given a person house whose house length is between variables s and t, and we have two trees, one is apple other one is orange. Since we have been given some distance of the fallen apple, and oranges respectively, what we have to do is to find those apples, and oranges whose distance falls in between the s and t
, or I must say which falls on xyz person's house.
Input Format
Code
static void countApplesAndOranges(int s, int t, int a, int b, int[] apples, int[] oranges) {
int appleSum=0, orangeSum=0, appleCount=0, orangeCount=0;
for(int i : apples){
appleSum = Math.abs(a+i);
if(appleSum>=s && appleSum<=t)
appleCount++;
}
for(int j : oranges){
orangeSum = Math.abs(b+j);
if(orangeSum>=s && orangeSum<=t)
orangeCount++;
}
System.out.print(appleCount + "\n" + orangeCount);
}
This is my piece of code which works fine on most of the test cases, but this case actually confused to hell. I'm actually writing down the gist of one of the failed test cases, and will give you the link for the same if it works out well for you guys.
Passed Test Case {1}
7 11
5 15
3 2
-2 2 1
5 -6
Passed Test Case {2}
7 10
4 12
3 3
2 3 -4
3 -2 -4
Failed Test Case
37455 87275
35609 89610
73201 77971
19736 19374 -68796 0 -68800 -80005 -88383 -8147 73241 -33256 20227 0
41620 30182 -95883 -88718 93989 44405 66495 87717 100000 -99845 -63634
98450 -63318 23501 -39150 22335 4955 -98587 -13608 -95388 -41752 4959
22375 -20025 -72101 -90667 -41569 94758 -26725 -53444 -8783 -81879
57041 23682 -60064 -23505 2850 96653 18487 -6644 -90710 71994 21513
36066 -65894 -9897 -86990 -97347 89784 88447 93133 12662 61685 -22914
-39075 -96807 -80465 -53820 36851 -51794 -11967 36658 -75592 22004 -961
66645 -93123 -65326 81871 -21785 -48242 -63552 32509 51078 -37656
-14966 4017 -58411 9346 13544 -63028 -93738 93924 68463 55032 -10046
87907 -20967 78972 85338 19584 45460 84382 -34690 -82301 14093 -60802
4170 -90955 92241 -34932 68913 -22262 49469 -45729 7942 65753 17354
-28647 93058 -43811 21411 8543 -44799 -71815 -40743 60445 -66946 -85090
-96873 97385 -15317 54454 -21021 -60256 -41301 -98896 -97184 63098
-60230 41376 42273 45807 58041 54260 21196 -85191 85267 -28305 30220
-76826 82999 72627 7{-truncated-}
Expected Output
89610
19582
There are more to this and the link is here : Test Case Inputs
PLEASE NOTE : I have not asked for the solution actually, my code works but I don't know why this logic fails for the inputs like this.
Any help would be appreciated! Thanks :)
EDITS
I have tried using the long
in place of int
, in case for the larger value like the one which is there in the failed test case, but again it got failed!
long appleSum=0, orangeSum=0, appleCount=0, orangeCount=0;
This code passed all test cases, what went wrong for you is you are using Math.abs()
,you shouldn't do that because there can be negative values of sum as well.
static void countApplesAndOranges(int s, int t, int a, int b, int[] apples, int[] oranges) {
int appleCount = 0;
int orangeCount = 0;
for(int i:apples){
if(s<=i+a && i+a<=t)
appleCount++;
}
for (int j : oranges) {
if (s <= j + b && j+b <= t)
orangeCount++;
}
System.out.println(appleCount);
System.out.println(orangeCount);
}