Skip to main content

Posts

Showing posts from October, 2022

How to find Factorials of Extra Long Number in Java | Factorial of BigInterger

Find Factorial of Extra Long Number | Factorial of BigInteger in Java | HackerRank Problem Problem Description : Calculate and print the factorial of a given integer.  The factorial of the integer n, written n!, is defined as: n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1 Example 1 : n = 30  ans = 265252859812191058636308480000000 Explanation = 30 * 29 * 28 * 27 * .... * 3 * 2 *1 Example 2 :  n = 50 ans = 30414093201713378043612608166064768844377641568960512000000000000 We can not store large number in Long data type as well. So we need BigInteger for that.  BigInteger class presents in java.math package. For use BigInteger we need to import math package first. Checkout How we can divide and compute modulo of BigInteger in Java : Divide and compute Modulo of large number Lets solve long number factorial problem. Solution 1 : Factorial of Extra long number in Java import java.math.BigInteger; import java.util.Scanner; public class ExtraLongFactorials {  ...

Bill Division HackerRank Solution in Java and Python with Explanation

Java and Python solution for Bill Division HackerRank problem Problem Description : Two friends Anna and Brian, are deciding how to split the bill at a dinner. Each will only pay for the items they consume. Brian gets the check and calculates Anna's portion. You must determine if his calculation is correct. For example, assume the bill has the following prices bill = [2, 4, 6]. Anna declines to eat item k = bill[2] which costs 6.  If Brian calculates the bill correctly, Anna will pay (2 + 4) / 2 = 3.  If he includes the cost of bill[2], he will calculate (2 + 4 + 6) / 2 = 6.  In the second case, he should refund 3 to anna. Complete the bonAppetit function in the editor below. It should print Bon Appetit if the bill is fairly split. Otherwise, it should print the integer amount of money that Brian owes Anna.  bonAppetit has the following parameter(s): bill : an array of integers representing the cost of each item ordered k : an integer representing the zer...

Climbing the Leaderboard HackerRank Solution in Java with Explanation

Java Solution for Climbing the Leaderboard HackerRank Problem Problem Description : An arcade game player wants to climb to the top of the leaderboard and track their ranking. The game uses Dense Ranking , so its leaderboard works like this:  The player with the highest score is ranked number 1 on the leaderboard.   Players who have equal scores receive the same ranking number, and the next player(s) receive the immediately following ranking number. Example 1 : ranked = [100, 90, 90, 80] player = [70, 80, 105] Output : [4, 3, 1] The ranked players will have ranks 1,2,2 and 3, respectively. If the player's scores are 70, 80 and 105, their rankings after each game are 4rth, 3rd and 1st. Return [4, 3, 1]. Example 2 : ranked = [100, 100, 50, 40, 40, 20, 10] player = [5, 25, 50, 120] The ranked players will have ranks 1,1,2,3,3,4,5 respectively. If the player's scores are 5, 25, 50 and 120, their rankings after each game are 6th, 4rth, 2nd and 1st. Return [6, 4, 2, 1]. Out...