Skip to main content

Posts

Showing posts from July, 2021

Number Line Jumps HackerRank solution in Java

Java solution for Number Line Jumps HackerRank problem Problem Description : You are choreographing a circus show with various animals. For one act, you are given two kangaroos on a number line ready to jump in the positive direction. The first kangaroo starts at location x1 and moves at a rate of v1 meters per jump.  The second kangaroo starts at location x2 and moves at a rate of v2 meters per jump.  You have to figure out a way to get both kangaroos at the same location at the same time as part of the show. If it is possible, return YES, otherwise return NO. Example : x1 = 2 v1 = 1 x2 = 1 v2 = 2 After one jump, they are both at x = 3, (x1 + v1 = 2 + 1, x2 + v2 = 1 + 2), so the answer is YES. See full description : Number Line Jump Problem Description   Solution 1 import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; imp

Apple and Orange Hackerrank solution in Java | Java 8 solution

Java 8 solution for Apple and Orange HackerRank 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 s is the start point, and t 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 a , and the orange tree is at point b . When a fruit falls from its tree, it lands d units of distance from its tree of origin along the x-axis. *A negative value of d means the fruit fell d units to the tree's left, and a positive value of d means it falls d units to the tree's right.   Given the value of d for m apples and n oranges, determine how many apples and oranges will fall on Sam's house (i.e., in the inclusive range [s, t])? For example, Sam&

Convert 24 to 12 Hour and 12 to 24 Hour Time in Java

Time Conversion in Java In Java, we can easily convert time from 12 to 24 hour and 24 to 12 hour.  12 hour time 12:00:00 becomes 00:00:00 in 24 hour time.  Lets jump on code. Example 1 : Convert 12 Hour to 24 Hour time in Java import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class TimeConversion {     public static void main(String[] args) {                 String s1 = "12:01:00 AM";         String s2 = "05:30:00 PM";                 DateFormat dateFormat = new SimpleDateFormat("hh:mm:ss aa");         DateFormat outputformat = new SimpleDateFormat("HH:mm:ss");         try {             Date date1 = dateFormat.parse(s1);             Date date2 = dateFormat.parse(s2);             System.out.println(outputformat.format(date1));             System.out.println(outputformat.format(date2));         } catch (ParseException e) {             e.printStackTrace();         }     } }

Mini-Max Sum HackerRank solution in Java

Find the minimum and maximum values that can be calculated by summing exactly four of the five integers Problem Description : Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.   Example 1 : arr = [1, 3, 5, 7, 9] The minimum sum is 1 + 3 + 5 + 7 = 16 and the maximum sum is 3 + 5 + 7 + 9 = 24. Answer is 16 24 Example 2 : arr = [396285104, 573261094, 759641832, 819230764, 364801279] The minimum sum is 364801279 + 396285104 + 573261094 + 759641832 = 2093989309 and the maximum sum is 396285104 + 573261094 + 759641832 + 819230764 = 2548418794. Answer is 2093989309 2548418794 Lets see solution. Solution 1 import java.util.ArrayList; import java.util.Collections; import java.util.List; public class MiniMaxSum {     public static void main(String[] args) {                 List<Integer> arr = new

Plus Minus HackerRank Solution in Java | Programming Blog

Java Solution for HackerRank Plus Minus Problem Given an array of integers, calculate the ratios of its elements that are positive , negative , and zero . Print the decimal value of each fraction on a new line with 6 places after the decimal. Example 1 : array = [1, 1, 0, -1, -1] There are N = 5 elements, two positive, two negative and one zero. Their ratios are 2/5 = 0.400000, 2/5 = 0.400000 and 1/5 = 0.200000. Results are printed as:  0.400000 0.400000 0.200000 proportion of positive values proportion of negative values proportion of zeros Example 2 : array = [-4, 3, -9, 0, 4, 1]  There are 3 positive numbers, 2 negative numbers, and 1 zero in array. Following is answer : 3/6 = 0.500000 2/6 = 0.333333 1/6 = 0.166667 Lets see solution Solution 1 import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.st

Diagonal Difference in Java | Find difference between sums of two diagonals | HackerRank problem

Java Solution for HackerRank Diagonal Difference problem  Problem Description : Given a square matrix, calculate the absolute difference between the sums of its diagonals.  For example, the square matrix arr is shown below:  1 2 3 4 5 6 9 8 9 The left to right diagonal = 1 + 5 + 9 = 15 The right to left diagonal = 3 + 5 + 9 = 17 Their absolute difference is | 15 - 17 | = 2. See full description : Diagonal Difference HackerRank Problem Description   Sample Input : Row and Column Size : 3 2 4 6 1 3 5 7 8 -9 Sample Output : 2 Explanation : left to right diagonal = 2 + 3 - 9 = -4. The right to left diagonal = 6 + 3 + 7 = 16. So the absolute difference is | -4 - 16 = 20 | Lest see solution and its explanation. Solution 1 import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.stream.Collectors.joining; im

Compare the Triplets | HackerRank Solution in Java with Explanation

Java Solution for Compare the Triplets HackerRank Problem Problem Description: Alice and Bob each created one problem for HackerRank. A reviewer rates the two challenges, awarding points on a scale from 1 to 100 for three categories: problem clarity , originality , and difficulty . The rating for Alice's challenge is the triplet a = (a[0], a[1], a[2]) , and the rating for Bob's challenge is the triplet b = (b[0], b[1], b[2]) . The task is to find their comparison points by comparing a[0] with b[0] , a[1] with b[1] , and a[2] with b[2] .   If a[i] > b[i], then Alice is awarded 1 point.  If a[i] < b[i], then Bob is awarded 1 point.   If a[i] = b[i], then neither person receives a point. Comparison points is the total points a person earned. Given a and b , determine their respective comparison points.  See Full description : Compare the Triplets Problem Description   Sample Explanation : a = [1, 2, 3] b = [3, 2, 1] For elements 0, Bob is awarded a point be

Method Overloading in Java | Type Promotion | Ambiguous Problem | Var args in Method Overloading

  What is Method Overloading? Method Overloading means one class have same methods name but have different parameters or arguments. In simple term, when any class contains same method name in class but with different parameters it is called Method Overloading in Java. In another simple term, using Method overloading in java we can create multiple same name methods in one class. In Method Overloading, method resolution always takes care by compiler based on method parameters. So it is also called Compile Time Polymorphism or Static Polymorphism or Early Binding. Learn more about Polymorphism and OOPs concept : Object Oriented Programming (OOP) concept in Java with Examples   In Method Overloading, method signature must be different. Means if we write same method signature in same class it gives compile time error. What is Method Signature? In java, method signature contains only method name followed by argument types. following code is called method signature in java.  Note : In java,