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(); } }
brand cialis 5mg generic cialis online medicine for erectile
buy cefadroxil 500mg for sale lamivudine price order propecia 5mg generic
order generic fluconazole 200mg purchase ciprofloxacin online cheap cipro canada
flagyl 200mg us buy bactrim 960mg pill oral keflex 250mg
order cleocin pills cheap clindamycin buy sildenafil generic
nolvadex us purchase ceftin without prescription order cefuroxime 250mg for sale
order generic sildenafil 100mg sildenafil 100mg canada buy generic sildalis online
azipro usa buy prednisolone 10mg pills buy gabapentin generic
order lasix for sale order furosemide 100mg without prescription purchase albuterol without prescription
levitra 20mg pills buy generic vardenafil 20mg buy plaquenil 200mg pills
buy ramipril generic arcoxia 120mg usa arcoxia cost
vardenafil 20mg tablet buy vardenafil 20mg pill brand plaquenil 400mg
mesalamine 400mg cost order astelin sprayers order avapro 150mg online cheap
buy benicar 20mg without prescription buy calan 240mg generic depakote 250mg generic
purchase temovate generic order cordarone without prescription buy cordarone paypal
order coreg 25mg aralen tablet chloroquine 250mg ca
order olumiant 2mg pills baricitinib 2mg pills atorvastatin 10mg brand
buy singulair sale cost avlosulfon 100mg order dapsone generic
how to buy amlodipine lisinopril online omeprazole 20mg generic
lopressor 50mg sale medrol buy online methylprednisolone 8 mg online
buy triamcinolone 10mg generic generic triamcinolone buy claritin pills for sale
sumycin over the counter purchase lioresal without prescription buy baclofen sale
buy bactrim generic where to buy cephalexin without a prescription order cleocin 300mg pill
buy erythromycin 500mg sale order nolvadex 20mg sale nolvadex 20mg drug
purchase budesonide without prescription rhinocort us bimatoprost for sale
buy methocarbamol 500mg pill order robaxin online cheap buy suhagra 100mg pill
celebrex 100mg for sale buy celecoxib tablets ondansetron 8mg oral
buy lamictal 50mg generic buy lamotrigine 50mg pills minipress 2mg over the counter
where to buy spironolactone without a prescription oral aldactone 25mg order valacyclovir 500mg without prescription
tretinoin for sale online cost avana buy avanafil pills
buy tadacip 20mg pill tadacip price buy indocin 75mg online cheap
terbinafine 250mg cost buy trimox 500mg online amoxicillin pill
buy cheap antivert meclizine 25mg for sale purchase minocin pill
buy erection pills order sildenafil 100mg for sale sildenafil 50mg tablets
erection pills online buy tadalafil 40mg pill tadalafil 20mg price
hytrin pills buy generic actos purchase cialis
cordarone for sale online order generic carvedilol 6.25mg buy dilantin tablets
avapro 150mg price temovate where to buy buspirone 5mg drug
buy oxybutynin 5mg without prescription ditropan 5mg brand order fosamax 70mg without prescription
brand albenza order albendazole pills order medroxyprogesterone 10mg sale
buy nitrofurantoin for sale nortriptyline 25 mg canada nortriptyline 25 mg ca
buy anacin tablets cheap pepcid buy famotidine generic
tacrolimus 5mg cost order mirtazapine pills order ropinirole for sale
rocaltrol sale buy rocaltrol 0.25mg fenofibrate over the counter
diovan where to buy oral diovan 160mg order ipratropium 100 mcg online
trileptal order online buy ursodiol paypal ursodiol 300mg price
brand decadron buy linezolid 600 mg generic starlix 120 mg usa
zyban pill order zyban 150mg pill brand strattera
order seroquel 50mg generic cost seroquel cheap lexapro 20mg
buy prozac generic femara 2.5 mg sale purchase femara
where to buy bisoprolol without a prescription bisoprolol 10mg generic terramycin 250mg generic
vantin cost flixotide drug purchase flixotide
cialis tadalafil 20mg sildenafil uk sildenafil 50mg uk
order acarbose sale brand precose griseofulvin 250mg cost
buy mintop generic order cialis 5mg sale where can i buy ed pills
buy aspirin 75 mg for sale buy aspirin 75mg without prescription brand zovirax
dipyridamole 100mg brand brand plendil pravachol uk
order generic meloset 3mg aygestin price purchase danocrine sale
buy fludrocortisone 100mcg pills order generic aciphex purchase imodium generic
monograph 600 mg tablet buy etodolac 600mg sale pletal medication
prasugrel 10mg us buy dimenhydrinate online cheap buy detrol for sale
ferrous 100mg cost cheap ascorbic acid buy sotalol 40 mg generic
purchase mestinon online piroxicam order rizatriptan 5mg without prescription
buy vasotec generic buy casodex 50 mg generic buy cheap generic duphalac
order generic latanoprost zovirax over the counter exelon pills
betahistine 16mg for sale benemid 500 mg tablet order probenecid 500 mg pills
premarin 0.625mg price viagra 25mg price sildenafil 50mg us
micardis 80mg brand molnupiravir cost buy molnunat
oral tadalafil 10mg buy cialis 10mg for sale sildenafil professional
generic cenforce 100mg buy naprosyn online aralen 250mg over the counter
buy cefdinir medication omnicef uk order prevacid 30mg generic
purchase provigil without prescription provigil pills prednisone 10mg without prescription