Posts

Lonely Integer | Hacker Rank in Java 7

Problem:  You will be given an array of integers. All of the integers except one occur twice. That one is unique in the array. Given an array of integers, find and print the unique element. For example,  , the unique element is  . Solution: import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Solution {     // Complete the lonelyinteger function below.     static int lonelyinteger(int[] a) {                          int n=a.length;int res=0;         for(int i=0;i<n;i++)         { res^=a[i];             }             return(res);     }     private static final Scanner scanner = new Scanner(System.in);     public static void main(String[] args...

Time Conversion | Hacker Rank Solution in Java 7

Problem: Given a time in  -hour AM/PM format , convert it to military (24-hour) time. Note: Midnight is 12:00:00AM on a 12-hour clock, and 00:00:00 on a 24-hour clock. Noon is 12:00:00PM on a 12-hour clock, and 12:00:00 on a 24-hour clock. Solution: import java.io.*; import java.util.*; public class Solution {     public static void main(String[] args) {         /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */         Scanner input = new Scanner(System.in);         String time = input.nextLine();         int hour = Integer.parseInt(time.substring(0,2));         int minute = Integer.parseInt(time.substring(3,5));         int second = Integer.parseInt(time.substring(6,8));         String meridiem = time.substring(8,10);                ...

Caesar Cipher: Encryption | Hacker Rank Solution in Java 7

Problem: Julius Caesar protected his confidential information by encrypting it in a cipher. Caesar's cipher rotated every letter in a string by a fixed number,  , making it unreadable by his enemies. Given a string,  , and a number,  , encrypt   and print the resulting string. Note:  The cipher  only  encrypts letters; symbols, such as  - , remain unencrypted. Solution: import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Solution {     private static final Scanner scanner = new Scanner(System.in);     public static void main(String[] args) {         int n = scanner.nextInt();         scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");         String s = scanner.nextLine();         int k = scanner.nextInt();       ...

Mini Max Sum | Hacker Rank Solution in Java

Problem: 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. For example,  . Our minimum sum is   and our maximum sum is  . We would print 16 24 Solution: import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Solution {     static void miniMaxSum(int[] arr) { int l=arr.length; //sort for(int i=0;i<l;i++) {     for(int j=i+1;j<l;j++)     {         if(arr[i]>arr[j])         {int temp;         temp=arr[i];         arr[i]=arr[j];         arr[j]=temp;         }     } } long min=0; long max=0; for(int i=...

Birthday Cake Candles | Hacker Rank Solution in Java 7

Problem:  Y ou are in charge of the cake for your niece's birthday and have decided the cake will have one candle for each year of her total age. When she blows out the candles, she’ll only be able to blow out the tallest ones. Your task is to find out how many candles she can successfully blow out. For example, if your niece is turning   years old, and the cake will have   candles of height  ,  ,  ,  , she will be able to blow out   candles successfully, since the tallest candles are of height   and there are   such candles. Solution: import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Solution {     // Complete the birthdayCakeCandles function below.     static int birthdayCakeCandles(int[] ar) { int n=ar.length;int max=ar[0]; for(int i=0;i<n;i++) {     if(ar[i...

Stair Case | Hacker Rank Solution in Java 7

Problem: Consider a staircase of size  : # ## ### #### Observe that its base and height are both equal to  , and the image is drawn using  #  symbols and spaces.  The last line is not preceded by any spaces. Write a program that prints a staircase of size  . Solution: import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Solution {     // Complete the staircase function below.     static void staircase(int n) { for(int i=0;i<n;i++) {     for(int k=n-1;k>i;k--)     {         System.out.print(" ");     }     for(int j=0;j<=i;j++)     {         System.out.print("#");     }     System.out.println(); }     }     private static final Scanner scanner = new Scanner(System.in);   ...

Plus Minus | Hacker Rank

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   places after the decimal. Note:  This challenge introduces precision problems. The test cases are scaled to six decimal places, though answers with absolute error of up to   are acceptable. Example There are   elements, two positive, two negative and one zero. Their ratios are  ,   and  . Results are printed as: 0.400000 0.400000 0.200000 Solution: import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Solution {     // Complete the plusMinus function below.     static void plusMinus(int[] arr) { float fp=0;float fn=0;float fo=0; int l=arr.length; for(int i=0;i<l;i++){ if(arr[i]>0) {     fp...