# Apple and Orange

# Apple and Orange

navjotahuja92 + 27 comments python way ;)

print(sum([1 for x in apple if (x + a) >= s and (x + a) <= t])) print(sum([1 for x in orange if (x + b) >= s and (x + b) <= t]))

chelBot + 2 comments omg...this is wonderful! I didn't know we could add 1 to a comprehension this way.

- JD
jonathandygert + 9 comments You can also use the fact that

`True == 1`

and`False == 0`

`print(sum(s <= a + d <= t for d in apples)) print(sum(s <= b + d <= t for d in oranges))`

nonotaka888 + 1 comment Great and simple way. I didn't know that. Thanks.

- AA
maxim_marquez + 4 comments This was my code, could anyone please help with what's the problem here?

def countApplesAndOranges(s, t, a, b, apples, oranges): apls = list(apples) orls = list(oranges) avf = [] ovf = [] for v in apls: if (a+v)>=s: avf.append(v) for g in orls: if (b+g)<=t: ovf.append(g) print len(avf) print len(ovf)

- AJ
jamesallenvinoy + 0 comments You will have to consider both 's' and 't' for the both the loops.

- RJ
ramrakesh64 + 0 comments Instead of appending to a list, you might have to take a variable with value 0 and then increment it in the IF loop. However, the problem with this kind of code(even I wrote the same) is this terminates because of the timeout error(though it is correct). First comment in this section gives an absolute answer.

- O
oneacg + 0 comments [deleted] - O
oneacg + 0 comments def countApplesAndOranges(s, t, a, b, apples, oranges): avf = [] ovf = [] for v in apples: if (a+v)>=s and (a+v)<=t: avf.append(v) for g in oranges: if (b+g)<=t and (b+g)>=s: ovf.append(g) print (len(avf)) print (len(ovf))

You dont have to create another list with in the definition. Also, as some one stated , you have to take both "s" and "t" into consideration. Remember we are trying to see if the apples fell with in the boundaries of "s" and "t".

- PB
tpb261 + 1 comment I don't even use apple and orange lists (explicitly)

print(sum([s<=int(apple_temp)+a<=t for apple_temp in input().strip().split(' ')]))

)

punithsk + 1 comment appleno = 0 orangeno = 0 for no in apple: if (no + a) in range(s,t+1): appleno += 1 for no in orange: if (no + b) in range(s,t+1): orangeno += 1 print appleno print orangeno

It says timed out. Can you tell me why. I have changed it to no+a >= s and no+a <= t. But i need to know why the former one doesn't work. Thank you

Kush131 + 2 comments Not sure if you eventually figured it out, but I tried the same approach and was having issues too. What fixed it for me was replacing "... in range(s,t)" with greater than or equal to & less than or equal to statements. It must be something specific with in or range that is causing issues.

- JD
jonathandygert + 2 comments Using

`x in range(s, t)`

means that for each check, you have to do worst case scenario (t-s-1) equality comparisons. Using`s <= x < t`

means that you do 2 comparisons for each check. klu_170030059 + 0 comments int main(){ int s; int t,count1=0,count2=0; scanf("%d %d",&s,&t); int a; int b; scanf("%d %d",&a,&b); int m; int n; scanf("%d %d",&m,&n); int temp; for(int apple_i = 0; apple_i < m; apple_i++){ scanf("%d",&temp); if(temp+a>=s && temp+a<=t) { count1++; } } for(int orange_i = 0; orange_i < n; orange_i++){ scanf("%d",&temp); if(temp+b>=s && temp+b<=t) { count2++; } } printf("%d\n",count1); printf("%d",count2); return 0; }

- JT
Jlookup + 3 comments OMG I'm switching

me = js; if (python == thisEasy){ me = python; }

SamRwanda + 0 comments Haha if you can switch please do it, Python is easy to fall in love with.

- AH
Unremarkable + 1 comment It isn't that much worse in JavaScript, except that JS Arrays don't have sum naturally.

Array.prototype.sum = function(f) { return this.reduce((s, v) => s + f(v), 0); } console.log( apple.sum(d => s - a <= d && d <= t - a)); console.log(orange.sum(d => s - b <= d && d <= t - b));

Or, just with reduce:

console.log( apple.reduce((sum, d) => sum + (s - a <= d && d <= t - a), 0)); console.log(orange.reduce((sum, d) => sum + (s - b <= d && d <= t - b), 0));

Or, by filtering instead:

console.log( apple.filter(d => s - a <= d && d <= t - a).length); console.log(orange.filter(d => s - b <= d && d <= t - b).length);

kidkkr + 0 comments I prefer map with filter. for more functionally speaking,

function addBy(num) { return (d) => d + num; } function isScored(d) { return s <= d && d <= t; } const larry = apples.map(addBy(a)).filter(isScored).length; const rob = oranges.map(addBy(b)).filter(isScored).length; console.log(larry, rob);

andrewsenner + 0 comments Careful,

`thisEasy`

could be a falsey value. :-)

ganugapatisaial1 + 0 comments hey! i did the same way though! python is great

diego_amicabile + 1 comment or len

print(len([x for x in apple if s <= x+a <= t ]), len([y for y in orange if s <= y+b <= t ]),sep = '\n')

ridoansalehnst + 0 comments what is the utility of 'x' before for loop ?

- SD
satyamdwivedi_s1 + 2 comments # include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

using namespace std;

int main(){ int s; int t; cin >> s >> t; int a; int b; cin >> a >> b; int m; int n;

`cin >> m >> n; int ctr1=0,ctr2=0; vector<int> apple(m); for(int apple_i = 0;apple_i < m;apple_i++){ cin >> apple[apple_i]; if(apple[apple_i]>0 && (s<=a+apple[apple_i] && a+apple[apple_i]<=t)) ctr1++; } vector<int> orange(n); for(int orange_i = 0;orange_i < n;orange_i++){ cin >> orange[orange_i]; if(orange[orange_i]<0 && (s<=b-abs(orange[orange_i]) && b-abs(orange[orange_i])<=t)) ctr2++; } cout<<ctr1<<"\n"<<ctr2; return 0;`

}

- DM
dahveed + 0 comments do you need to add the condition that apple[apple_i]>0 or orange[orange_i] <0? isn't this extra?

- M
milczarek_r + 0 comments You can move the code for calculating number of fruits on house to function to keep it DRY:

int CalculateNumberOfFruitsOnHouse(int s, int t, int tree, const std::vector<int> &fruits) { int rel_s = s - tree; int rel_t = t - tree; int num = 0; for(int fruit : fruits){ if(fruit >= rel_s && fruit <= rel_t) ++num; } return num; }

SEK7I0N + 0 comments [deleted]kbenriquez + 1 comment I'm not a Python expert but wouldn't your code create an entry into an array for every value whether it be true or false? I'm concerned about the performance so I created my code below. Feedback would be greatly appreciated as well as tips to improve my coding. :D

def appleAndOrange(s, t, a, b, apple, orange): x = len([1 for i in apple if i+a in range(s,t+1)]) y = len([1 for i in orange if i+b in range(s,t+1)]) return [x,y]

klu_170030059 + 1 comment peace.....brooo....

klu_170030059 + 0 comments lollll...............

adipandya + 0 comments Alternate Python solution, make a range over the length of the house...

r = range(s,t+1) print(sum([a+x in r for x in apples])) print(sum([b+x in r for x in oranges]))

SamRwanda + 0 comments Me too :)

himanshu_tiwari + 0 comments Here's another one ;)

print([d+a>=s and d+a<=t for d in apple].count(True))

print([b+d>=s and b+d<=t for d in orange].count(True))

[DELETED] + 0 comments [deleted]- FD
fabien_dumay + 3 comments Nice... The Java 8 is not bad too :) a bit longer, but easy to understand.

Arrays.stream(apple) .filter(x -> x + a >= s && x + a <= t) .count(); Arrays.stream(orange) .filter(x -> x + b >= s && x + b <= t) .count();

- FF
FilipF + 0 comments Yup yup, streams make things elegant. Array streams also give you an opportunity to use

`.parallel()`

. Another variation is to skip reading into an array first:long orangeHits = IntStream.generate(()->in.nextInt()) .limit(n) .filter(d -> d+b>=s && d+b<=t) .count();

stonyark + 0 comments This is amazing. Thank you :D

- AJ
ash_jo4444 + 0 comments How do you print the count?

abhishek106 + 0 comments this is amazing! thanks for sharing

- JT
Jlookup + 0 comments We are potentially dealing with a large number of large numbers. I don't want hold those big arrays in memory. I just want to evaulate each number and keep track of the count. Does this code accomplish that? Feedback please.

Python 3:

import sys def countHits(lHouse, rHouse, treeLoc): lDist = lHouse - treeLoc rDist = rHouse - treeLoc return sum(1 for spot in input().strip().split(' ') if hitHouse(int(spot), lDist, rDist)) def hitHouse(spot, lDist, rDist): if spot >= lDist and spot <= rDist: return True lHouse, rHouse = [int(x) for x in input().strip().split(' ')] aTree, oTree = [int(x) for x in input().strip().split(' ')] aCount, oCount = [int(x) for x in input().strip().split(' ')] print (countHits(lHouse, rHouse, aTree)) print (countHits(lHouse, rHouse, oTree))

lovereli + 1 comment more pythonic

print(sum([1 for each in apple if s <= (each + a) <=t])) print(sum([1 for each in orange if s <= (each + b) <=t]))

- KH
Slaunger + 0 comments No need for the list and no need for the '1'

s, t = map(int, input().split()) a, b = map(int, input().split()) _ = input() print(sum(s <= (a + i) <= t for i in map(int, input().split()))) print(sum(s <= (b + i) <= t for i in map(int, input().split())))

djs21 + 1 comment Swift way:

print(apples.filter{$0 + a >= s && $0 + a <= t}.count) print(oranges.filter{$0 + b >= s && $0 + b <= t}.count)

- JD
jonathandygert + 0 comments Ruby way:

puts apples.count { |apple| (a + apple).between?(s, t) } puts oranges.count { |orange| (b + orange).between?(s, t) }

coderai + 0 comments We can also use

`range`

function here :`print(sum([ 1 for app in apple if a+(app) in range(s,t+1)])) print(sum([ 1 for org in orange if b+(org) in range(s,t+1)]))`

- YS
yash71720 + 0 comments OMG!! this is simply awesome dude.... i never thought that python could be this much flexible

- MG
Ardillo + 0 comments apple = list(filter(lambda x: s-a <= x <= t-a, map(int, input().split()))) orange = list(filter(lambda x: s-b <= x <= t-b, map(int, input().split()))) [print(x) for x in map(len, [apple, orange])]

- SD
satyamdwivedi_s1 + 0 comments # include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

using namespace std;

int main(){ int s; int t; cin >> s >> t; int a; int b; cin >> a >> b; int m; int n;

`cin >> m >> n; int ctr1=0,ctr2=0; vector<int> apple(m); for(int apple_i = 0;apple_i < m;apple_i++){ cin >> apple[apple_i]; if(apple[apple_i]>0 && (s<=a+apple[apple_i] && a+apple[apple_i]<=t)) ctr1++; } vector<int> orange(n); for(int orange_i = 0;orange_i < n;orange_i++){ cin >> orange[orange_i]; if(orange[orange_i]<0 && (s<=b-abs(orange[orange_i]) && b-abs(orange[orange_i])<=t)) ctr2++; } cout<<ctr1<<"\n"<<ctr2; return 0;`

}

LeHarkunwar + 1 comment Did nearly the same

print(len([1 for app in apple if app >= s-a and app <= t-a])) print(len([1 for ora in orange if ora <= t-b and ora >= s-b]))

- KH
Slaunger + 1 comment No need for the '1', and the [] and the condition can be written simpler. Also no need to read the entire thing in as a list, which can be very memory consuming. Here using all iterators

... print(sum(s <= (a + i) <= t for i in map(int, input().split()))) print(sum(s <= (b + i) <= t for i in map(int, input().split())))

LeHarkunwar + 0 comments Yeah, that's a good way to do it, I also did this in another question.

- DP
dinesh_pi + 0 comments can someone explain the sum function and for function please

- MR
minLewis + 0 comments That is beautiful, I feel like such a barbarian...

app_cnt = 0 org_cnt = 0 for ap in apple: if ap > 0: if t >= a+ap >= s: app_cnt += 1 for og in orange: if og < 0: if t >= b+og >= s: org_cnt += 1 print(app_cnt) print(org_cnt)

- SC
suryach750 + 0 comments can you explain what is the use of "1" here

- MT
cryomick + 0 comments print(len([x for x in apple if x>0 and a+x>=s and a+x<=t])) print(len([y for y in orange if y<0 and b+y<=t and b+y>=s]))

I know that the savings are miniscule, but discarding the distances are to the left of the apple tree or to the right of the orange tree reduces the number of values to check.

- N
della90 + 0 comments You can also get rid of

`(x + a) >= s and (x + a) <= t`

and replace it with`s <= a + d <= t`

or`a + d in range(s, t+1)`

nimish_gupta + 0 comments [deleted]- MV
mahesh_vemula + 0 comments c = list(filter(lambda x: x==True, map(lambda x: a+x in range(s,t+1),apples))) d = list(filter(lambda x: x==True, map(lambda x: b+x in range(s,t+1),oranges))) print(str(len(c))+"\n"+str(len(d)))

Is this code worthy?

- TM
thma1 + 0 comments Python rocks!

- MS
mariojsanchez15 + 0 comments The solution is very elegant, but the complexity of the algorithm is being affected as a consequence.

- SN
srisamatha80 + 0 comments another way:) print(len([i for i in apples if i+a in range(s,t+1)])) print(len([i for i in oranges if i+b in range(s,t+1)]))

I_Shaunt + 0 comments print(sum((a + i) in range(s, t+1) for i in appleDistance)) print(sum((b + i) in range(s, t+1) for i in orangeDistance))

- CD
dimare_christian + 0 comments similarly, you could:

apples = map(lambda x: 1 if s <= a+x <= t else 0, apples) oranges = map(lambda x: 1 if s <= b+x <= t else 0, oranges) print(sum(apples), sum(oranges))

they're both pretty pythonic. but list comprehensions are bae

qwerty983 + 0 comments `int ax = apples.Where(x=>s<=(x+a)&&(x+a)<=t).Count(); int ox = oranges.Where(x=>s<=(x+b)&&(x+b)<=t).Count(); c# style ;)`

amylokh + 0 comments I love python man!

rshaghoulian + 13 comments ### Java solution - passes 100% of test cases

From my HackerRank solutions.

Runtime: O(m + n)

Space Complexity: O(1)Avoid using arrays to store values since that will take O(m + n) space.

import java.util.Scanner; public class Solution { public static void main(String[] args) { /* Read and save input */ Scanner scan = new Scanner(System.in); int s = scan.nextInt(); int t = scan.nextInt(); int a = scan.nextInt(); int b = scan.nextInt(); int m = scan.nextInt(); int n = scan.nextInt(); /* Calculate # of apples that fall on house */ int applesOnHouse = 0; for (int i = 0; i < m; i++) { int applePosition = a + scan.nextInt(); if (applePosition >= s && applePosition <= t) { applesOnHouse++; } } System.out.println(applesOnHouse); /* Calculate # of oranges that fall on house */ int orangesOnHouse = 0; for (int i = 0; i < n; i++) { int orangePosition = b + scan.nextInt(); if (orangePosition >= s && orangePosition <= t) { orangesOnHouse++; } } System.out.println(orangesOnHouse); scan.close(); } }

Let me know if you have any questions.

jathavedas + 1 comment Nice... I was using arrays and test results were failing for a few use cases. Thank you

rshaghoulian + 1 comment You're very welcome.

148W1A05B1 + 0 comments do you have answer using arrays?

darpan_dodiya + 1 comment Liked your variable name conventions!

rshaghoulian + 0 comments Thanks :)

sanjugodara13 + 1 comment Done in the same way but it is not passing some of the test cases

rshaghoulian + 1 comment Hi. Try to compare your code to mine line by line to see what we have done differently. Or, try to use portions of my code in your code to narrow down where the bug may be.

sanjugodara13 + 0 comments Thanks but i had sorted out the bug at the same day .

sanjugodara13 + 0 comments [deleted]KhateebXtreme + 0 comments after seeing your code ..i found the bug..why my memory stack was hitting the bar of 50 kb ...thanks

- PC
thecodeavenger + 1 comment Can you explain why the code fails if it is done using arrays?

rshaghoulian + 0 comments I am assuming you want 1 array for apples, and another for oranges. This would take O(n+m) space. Using arrays in this way would require more space, since we would be saving positions of all the fruit in the function.

I think the code fails since HackerRank sets on the amount of memory/space they let you use in this challenge.

priyankap1022 + 1 comment Hey!! Can you once explain why does some cases fail when using arrays?

KhateebXtreme + 0 comments its all about space time complexity...duh most of those failing testcases due to timeout occurs due to this

- SP
pandeysdr16 + 1 comment can u plz explain this line?

int applePosition = a + scan.nextInt(); if (applePosition >= s && applePosition <= t)

rshaghoulian + 1 comment int applePosition = a + scan.nextInt();

The above line calcalates the apple's position by adding the variables

**a**and**d**(where d is read from input). The problem has a picture that shows what these correspond to.if (applePosition >= s && applePosition <= t)

The above line checks to see if the apple's position is in between

**s**and**t**. These 2 variables are also depicted in the problem's picture.- SP
pandeysdr16 + 0 comments ok , got it. Thank you.

- AK
ajayshakya + 1 comment please can u explain this code: int orangePosition = b + scan.nextInt();

- AH
LePetitRenard + 0 comments Basically the

**positionOfFallenOrange**equals the**positionOfOrangeTree**plus the**distanceFromTheOrangeTree****orangePosition**=**b**+**scan.nextInt()**;Hope it makes sense :)

- TC
tomcollier94 + 0 comments I did it the same way, however I left the arrays as getting rid of them didn't even cross my mind!

**-Thank You!** sudheera_nv + 0 comments wow, this is gread solution. something to lern as well

- OA
alfredooctavio + 1 comment I did it in the same way but I include more code for constraints. My code passes 100% test cases too. I saw that you does not include code for constraints. It is weird that your code passes 100% test cases, may be the test cases not include the constraints validation. Well congratulations your code is great.

Let me share my code, any comment is welcome:

public static void main(String[] args) { Scanner in = new Scanner(System.in); int s = in.nextInt(); int t = in.nextInt(); int a = in.nextInt(); int b = in.nextInt(); int m = in.nextInt(); int n = in.nextInt(); int[] datas = {s,t,a,b,m,n}; int apples = 0, oranges = 0; for(int data:datas){ if( (data <=1) && (data >= Math.pow(10,5)) ){ System.err.println("Data out of range."); System.exit(-1); } } if( (a > s) && (s > t) && (t > b )){ System.err.println("the data does not meet the requirements."); System.exit(-1); } int[] apple = new int[m]; for(int apple_i=0; apple_i < m; apple_i++){ apple[apple_i] = in.nextInt(); if( apple[apple_i] >= Math.pow(-10,5) && apple[apple_i] <= Math.pow(10,5)){ if( ( (a + apple[apple_i]) >= s) && ( (a + apple[apple_i]) <= t) ) apples++; } else{ System.err.println("Apple: Distance out of the range."+apple[apple_i]); System.exit(-1); } } int[] orange = new int[n]; for(int orange_i=0; orange_i < n; orange_i++){ orange[orange_i] = in.nextInt(); if( orange[orange_i] >= Math.pow(-10,5) && orange[orange_i] <= Math.pow(10,5)){ if( ( (b + orange[orange_i]) >= s) && ( (b + orange[orange_i]) <= t) ) oranges++; } else{ System.err.println("Orange: Distance out of the range."+orange[orange_i]); System.exit(-1); } } System.out.println(apples); System.out.println(oranges); }

rshaghoulian + 1 comment Good job. You don't need to account for constraints. By listing constraints in the problem statement, HackerRank ensures that the data they pass to your program will be within those constraints.

- OA
alfredooctavio + 0 comments Thank you for your comment I'll keep it in mind.

- ZA
zakiralig184 + 0 comments Nice! U r careful about space complexity. Go on your discussion.Thanks

rcgonzalezf + 1 comment You don't even have to store the array, you can do the counting inline while reading the input:

int apples = 0; for(int i=0; i < m; ++i){ long d = in.nextLong(); long p = d+a; if ( p >= s && p <= t ) ++apples; }

piyush14031 + 1 comment I think my solution is similar, but doesn't work for all input cases, any idea why?

//header files bla bla int main() { unsigned int s, t, a, b, m, n; cin>>s>>t; long long int house = (t-s); cin>>a>>b; cin>>m>>n; long long int d; long long int i,j; int count=0, count1=0; for (i=0;i<m;i++) { cin>>d; long long int x=(a+d); if (x>=s&&x<=t) count++; } for(j=0;j<n;j++) { cin>>d; long long int y=(b-d); if (y>=s && y<=t) count1++; } cout<<count<<endl<<count1; return 0; }

hvkushwaha + 0 comments remove unsigned you are just neglecting negative numbers, make it long long instead of unsigned

DavidODW + 1 comment javascript way :)

var apple_count = apple.filter(value => value + a >= s && value + a <= t).length; var orange_count = orange.filter(value => value + b >= s && value + b <= t).length;

- VV
vityavv + 0 comments That way might be quicker than mine but I think mine looks nicer

function appleAndOrange(s, t, a, b, apple, orange) { return [ apple.map(p => p + a).filter(p => p >= s && p <= t).length, orange.map(p => p + b).filter(p => p >= s && p <= t).length ] }

- WR
willrowe + 4 comments C

#include <stdio.h> #include <stdlib.h> int main(){ int s,t; scanf("%d %d",&s,&t); int a, b; scanf("%d %d",&a,&b); int m, n; scanf("%d %d",&m,&n); int *apple = malloc(sizeof(int) * m); int appleCount = 0; for(int i = 0; i < m; i++){ scanf("%d",&apple[i]); if(a+apple[i] >= s && a+apple[i] <= t){ appleCount++; } } int *orange = malloc(sizeof(int) * n); int orangeCount = 0; for(int i = 0; i < n; i++){ scanf("%d",&orange[i]); if(b+orange[i] >= s && b+orange[i] <= t){ orangeCount++; } } printf("%d\n%d", appleCount, orangeCount); return 0; }

radesh0430 + 0 comments awesome!

somayaji + 1 comment Why complicate things with arrays and pointers? This problem can be solved with neither in O(m+n) time and O(1) space:

int main(){ int s, t, m, n, a, b, apple = 0 , orange = 0, dist; scanf("%d %d %d %d %d %d", &s, &t, &a, &b, &m, &n); for(int i = 0; i < m; i ++) { scanf("%d", &dist); if (dist >= s - a && dist <= t - a) apple ++; } for(int i = 0; i < n; i ++) { scanf("%d", &dist); if (dist <= t - b && dist >= s - b) orange ++; } printf("%d\n%d", apple, orange); return 0; }

Gerard0 + 0 comments You could even get ride of the if() and do

apples += ( pos + a >= s && pos + a <= t );

- H
IS201601013 + 0 comments [deleted] - JL
jluisg_23 + 0 comments Similar approach, but using a function:

#include <stdio.h> #include <stdlib.h> int fruit_counter(int house0, int house1, int three, int* fruit, int num_fruits){ int fruit_loc, count = 0; for(int i = 0; i < num_fruits; ++i){ fruit_loc = three+fruit[i]; if(fruit_loc >= house0 && fruit_loc <= house1){ ++count; } } return count; } int main(){ int s, t, a, b, m ,n; int apple_count, orange_count; scanf("%d %d",&s,&t); scanf("%d %d",&a,&b); scanf("%d %d",&m,&n); int* apple = malloc(m*sizeof(int)); for(int i = 0; i < m; ++i){ scanf("%d",&apple[i]); } int *orange = malloc(n*sizeof(int)); for(int i = 0; i < n; ++i){ scanf("%d",&orange[i]); } apple_count = fruit_counter(s, t, a, apple, m); orange_count = fruit_counter(s, t, b, orange, n); printf("%d\n%d\n", apple_count, orange_count); return 0; }

kakyoin01 + 0 comments Slight inconsistency in problem output instructions. The problem description says to output "

*2 space-separated integers*" e.g.:**on a line**: Larryâ€™s score followed by Robâ€™s score1 1

...but expected output for base test case when I tested this with JavaScript (NodeJS) is:

1 1

mhelvens + 0 comments The wrong output format description is given:

*"Print two space-separated integers on one line of output."*The example and actual problem expect them to be newline separated.

- MA
ratherauqib + 0 comments Dont waste memory

import java.io.

*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*;public class Solution {

`public static void main(String[] args) { int i=0,j=0; Scanner in = new Scanner(System.in); int s = in.nextInt(); int t = in.nextInt(); int a = in.nextInt(); int b = in.nextInt(); int m = in.nextInt(); int n = in.nextInt(); int apple; for(int apple_i=0; apple_i < m; apple_i++){ apple = in.nextInt(); if((a+apple)>=s && (a+apple)<=t) i++; } int orange; for(int orange_i=0; orange_i < n; orange_i++){ orange = in.nextInt(); if((b+orange)<=t && (b+orange)>=s) j++; } System.out.println(i); System.out.println(j); }`

}

dsncode + 0 comments scala way:

println(apple.map{fruit => fruit+a}.filter(_>=s).filter(_<=t).size)

- SS
sachin_srikanta1 + 0 comments print(len(list(filter(lambda x:x+a in range(s,t+1),apple)))) print(len(list(filter(lambda x:b+x in range(s,t+1),orange))))

Sort 577 Discussions, By:

Please Login in order to post a comment