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.
Avoid using arrays to store values since that will take O(m + n) space.
importjava.util.Scanner;publicclassSolution{publicstaticvoidmain(String[]args){/* Read and save input */Scannerscan=newScanner(System.in);ints=scan.nextInt();intt=scan.nextInt();inta=scan.nextInt();intb=scan.nextInt();intm=scan.nextInt();intn=scan.nextInt();/* Calculate # of apples that fall on house */intapplesOnHouse=0;for(inti=0;i<m;i++){intapplePosition=a+scan.nextInt();if(applePosition>=s&&applePosition<=t){applesOnHouse++;}}System.out.println(applesOnHouse);/* Calculate # of oranges that fall on house */intorangesOnHouse=0;for(inti=0;i<n;i++){intorangePosition=b+scan.nextInt();if(orangePosition>=s&&orangePosition<=t){orangesOnHouse++;}}System.out.println(orangesOnHouse);scan.close();}}
Apple and Orange
You are viewing a single comment's thread. Return to all 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.
Let me know if you have any questions.