Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank, Algorithm Solutions of Problem Solving Section in Java. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.
In this post, you will find the solution for Apple and Orange in Java-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.
Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.
Introduction To Algorithm
The word Algorithm means “a process or set of rules to be followed in calculations or other problem-solving operations”. Therefore Algorithm refers to a set of rules/instructions that step-by-step define how a work is to be executed upon in order to get the expected results.
Advantages of Algorithms:
- It is easy to understand.
- Algorithm is a step-wise representation of a solution to a given problem.
- In Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the programmer to convert it into an actual program.
Link for the Problem – Apple and Orange – Hacker Rank Solution
Apple and Orange – Hacker Rank Solution
Problem:
Sam’s house has an apple tree and an orange tree that yield an abundance of fruit. Using the information given below, determine the number of apples and oranges that land on Sam’s house.
In the diagram below:
- The red region denotes the house, where is the start point, and is the endpoint. The apple tree is to the left of the house, and the orange tree is to its right.
- Assume the trees are located on a single point, where the apple tree is at point , and the orange tree is at point .
- When a fruit falls from its tree, it lands units of distance from its tree of origin along the -axis. *A negative value of means the fruit fell units to the tree’s left, and a positive value of means it falls units to the tree’s right. *
![Apple and Orange in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct] 2 Apple and orange(2).png](https://s3.amazonaws.com/hr-challenge-images/25220/1474218925-f2a791d52c-Appleandorange2.png)
![Apple and Orange in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct] 3 image 51](https://technorj.com/wp-content/uploads/2021/12/image-51.png)
1 2
Function Description
Complete the countApplesAndOranges function in the editor below. It should print the number of apples and oranges that land on Sam’s house, each on a separate line.
countApplesAndOranges has the following parameter(s):
- s: integer, starting point of Sam’s house location.
- t: integer, ending location of Sam’s house location.
- a: integer, location of the Apple tree.
- b: integer, location of the Orange tree.
- apples: integer array, distances at which each apple falls from the tree.
- oranges: integer array, distances at which each orange falls from the tree.
Input Format
The first line contains two space-separated integers denoting the respective values of and .
The second line contains two space-separated integers denoting the respective values of and .
The third line contains two space-separated integers denoting the respective values of and .
The fourth line contains space-separated integers denoting the respective distances that each apple falls from point .
The fifth line contains space-separated integers denoting the respective distances that each orange falls from point .
Constraints
![Apple and Orange in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct] 4 image 52](https://technorj.com/wp-content/uploads/2021/12/image-52.png)
Output Format
Print two integers on two different lines:
- The first integer: the number of apples that fall on Sam’s house.
- The second integer: the number of oranges that fall on Sam’s house.
Sample Input 0
7 11 5 15 3 2 -2 2 1 5 -6
Sample Output 0
1 1
Explanation 0
![Apple and Orange in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct] 5 image 50](https://technorj.com/wp-content/uploads/2021/12/image-50.png)
Apple and Orange – Hacker Rank Solution
import java.util.Scanner; /** * @author Techno-RJ * */ public class AppleAndOrange { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int s = sc.nextInt(); int t = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); int m = sc.nextInt(); int n = sc.nextInt(); int aCount = 0, oCount = 0; for (int i = 0; i < m; i++) { int temp = sc.nextInt(); if ((a + temp) >= s && (a + temp) <= t) { aCount++; } } for (int i = 0; i < n; i++) { int temp = sc.nextInt(); if ((b + temp) >= s && (b + temp) <= t) { oCount++; } } System.out.println(aCount); System.out.println(oCount); sc.close(); } }