We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Apple and Orange
Apple and Orange
+ 84 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]))
+ 23 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.
+ 10 comments 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;
+ 1 comment For the one who creates this challenge !!!!!
Why can't you explain clearly your challenge . when I see the answer it is a just simple code But understanding the question is the basis to answer correctly
Kindly rewrite your question
Thanks
+ 6 comments C++ Solution , any optimizations please suggest :
void countApplesAndOranges(int s, int t, int a, int b, vector<int> apples, vector<int> oranges) { int m = apples.size(); int n = oranges.size(); int appCount=0; int orgCount=0; for(int i =0; i< m ; i++) { int appSum = a + apples[i]; if(appSum >= s && appSum <= t) appCount++; } for(int i =0; i< n ; i++) { int orgSum = b + oranges[i]; if(orgSum >= s && orgSum <= t) orgCount++; } cout<<appCount<<endl; cout<<orgCount<<endl; }
`
Load more conversations
Sort 2201 Discussions, By:
Please Login in order to post a comment