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.
TypeScript Solution
1. Declare orange and apple amount variables
2. Declare pointer variable
3. Loops until pointer is length of apple array - 1 while checking if apples and oranges aat that index are within the range of s to t
4. Checks if there are more oranges in the orange array
5. Begins to loop through the oranges array from where it left off in the last loop
6. Console log apple and orange amounts
functioncountApplesAndOranges(s:number,t:number,a:number,b:number,apples:number[],oranges:number[]):void{letorangeAmount=0;letappleAmount=0;letpointer:number;for(pointer=0;pointer<apples.length;pointer++){// Check if apples[pointer] && (apples[pointer] >= s && apples[pointer] <= t)// Increment appleAmount// Check if oranges[pointer] && (oranges[pointer] >= s && apples[pointer] <= t)// Increment orangeAmountif(apples[pointer]&&(apples[pointer]+a>=s&&apples[pointer]+a<=t)){appleAmount+=1;}if(oranges[pointer]&&(oranges[pointer]+b>=s&&oranges[pointer]+b<=t)){orangeAmount+=1;}}// Check if pointer < oranges.length - 1 // Loop though and apply the same if statemtns as the previous loop// console log appleAmount// console log orangeAmountif(pointer<oranges.length-1){for(;pointer<oranges.length;pointer++){if(oranges[pointer]&&(oranges[pointer]+b>=s&&oranges[pointer]+b<=t)){orangeAmount+=1;}}}console.log(appleAmount);console.log(orangeAmount);}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Apple and Orange
You are viewing a single comment's thread. Return to all comments →
TypeScript Solution 1. Declare orange and apple amount variables 2. Declare pointer variable 3. Loops until pointer is length of apple array - 1 while checking if apples and oranges aat that index are within the range of s to t 4. Checks if there are more oranges in the orange array 5. Begins to loop through the oranges array from where it left off in the last loop 6. Console log apple and orange amounts