Posts

Showing posts with the label Java 7

Counter game | Hacker Rank in C

Problem: Louise and Richard have developed a numbers game. They pick a number and check to see if it is a power of  . If it is, they divide it by  . If not, they reduce it by the next lower number which is a power of  . Whoever reduces the number to   wins the game. Louise always starts. Given an initial value, determine who wins the game. As an example, let the initial value  . It's Louise's turn so she first determines that   is not a power of  . The next lower power of   is  , so she subtracts that from   and passes   to Richard.   is a power of  , so Richard divides it by   and passes   to Louise. Likewise,   is a power so she divides it by   and reaches  . She wins the game. Update  If they initially set counter to  , Richard wins. Louise cannot make a move so she loses. Solution: #include <assert.h> #include <limits.h> #i...

Maximizing XOR | Hacker Rank in Java 7

Problem: Given two integers,   and  , find the maximal value of    xor   , written  , where   and   satisfy the following condition: For example, if   and  , then Our maximum value 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 maximizingXor function below.     static int maximizingXor(int l, int r) {         int res=0;int max=0;         for(int i=l;i<=r;i++)         {           for(int j=l;j<=r;j++)                {                   res=i^j;             if (max<=res)            ...

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=...