Friday, December 16, 2016

Compare the Triplet

Alice and Bob each created one problem for HackerRank. A reviewer rates the two challenges, awarding points on a scale from to for three categories: problem clarity, originality, and difficulty.
We define the rating for Alice's challenge to be the triplet , and the rating for Bob's challenge to be the triplet .
Your task is to find their comparison scores by comparing with , with , and with .
  • If , then Alice is awarded point.
  • If , then Bob is awarded point.
  • If , then neither person receives a point.
Given and , can you compare the two challenges and print their respective comparison points?


Solution : 

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int alice=0;
        int bob=0;
        int a0 = in.nextInt();
        int a1 = in.nextInt();
        int a2 = in.nextInt();
        int b0 = in.nextInt();
        int b1 = in.nextInt();
        int b2 = in.nextInt();
       
        if (a0<b0){
            bob+=1;
           
        }
        if (a1<b1){
            bob+=1;
           
        }
        if (a2<b2){
            bob+=1;
          
        }
        if(a0>b0 ){
            alice+=1;
          
           
        }
        if(a1>b1 ){
            alice+=1;
           
           
        }
        if(a2>b2){
            alice+=1;
           
           
        }
        if(a0==b0){
            alice+=0;
           bob+=0;         
          
        }
        if(a1==b1){
            alice+=0;
           bob+=0;         
          
        }
        if(a2==b2){
            alice+=0;
           bob+=0;         
          
        }
        System.out.println(alice+" "+bob);
    }
}

Diagonal Difference

Given a square matrix of size , calculate the absolute difference between the sums of its diagonals.
Input Format
The first line contains a single integer, . The next lines denote the matrix's rows, with each line containing space-separated integers describing the columns.
Output Format
Print the absolute difference between the two sums of the matrix's diagonals as a single integer.


Solution :


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
       
       
        int a[][] = new int[n][n];
        for(int a_i=0; a_i < n; a_i++){
            for(int a_j=0; a_j < n; a_j++){
                a[a_i][a_j] = in.nextInt();
            }
        }
        System.out.println(diffrence(a,n));      
       
    }
     static int diffrence(int a[][], int n){
        int sumi=0;
        int sumj=0;
       
        for(int i=0;i<a.length;i++){
            for(int j=0;j<a.length;j++){
                if(i==j){
                    sumi+=a[i][j];
                }
                if(j==(n-i-1))
                sumj+=a[i][j];
            }
        }     
         return Math.abs(sumi-sumj);
    }
   
}

Kangaroo

There are two kangaroos on an x-axis ready to jump in the positive direction (i.e, toward positive infinity). The first kangaroo starts at location and moves at a rate of meters per jump. The second kangaroo starts at location and moves at a rate of meters per jump. Given the starting locations and movement rates for each kangaroo, can you determine if they'll ever land at the same location at the same time?


Solution :

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int x1 = in.nextInt();
        int v1 = in.nextInt();
        int x2 = in.nextInt();
        int v2 = in.nextInt();
        int x1sum=x1;
        int x2sum=x2;
        boolean test=false;
      
       while (x1sum<x2sum || x1sum>=0 && x1sum<=10000  || x2sum>=0 && x2sum<=10000)  {
            x1sum=x1sum+v1;
            x2sum=x2sum+v2;
           
            if(x1sum==x2sum){
                test=true;
                System.out.println("YES");break;
            }
        }
        if(test==false){
            System.out.println("NO");
        }
       
    }
}
 

Circular Array Rotation

John Watson performs an operation called a right circular rotation on an array of integers, . After performing one right circular rotation operation, the array is transformed from to .
Watson performs this operation times. To test Sherlock's ability to identify the current element at a particular position in the rotated array, Watson asks queries, where each query consists of a single integer, , for which you must print the element at index in the rotated array (i.e., the value of ).
Input Format
The first line contains space-separated integers, , , and , respectively.
The second line contains space-separated integers, where each integer describes array element (where ).
Each of the subsequent lines contains a single integer denoting .


Solution :

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
       Scanner scn=new Scanner(System.in);
        int n=scn.nextInt();
        int k=scn.nextInt();
        int q=scn.nextInt();
        if(n>=1 && n<=100000 || k>=1 && k<=100000 || q>=1 && q<=500 )
            {      
                int temp=0;
                int nm=1;
               
                int a[]=new int [n];
                int b[] = new int[n];
                for(int i=0;i<a.length;i++){
                    a[i]=scn.nextInt();
                }
               
                for(int i=0;i<n;i++){
                    temp = k+i;
                    if(temp>=n){
                        temp = temp%n;
                    }
                  
                    b[temp]=  a[i];
                   
                }
                for(int i=0;i<q;i++){
                    int num=scn.nextInt();
                    if(num <n){
                       System.out.println(b[num]);
                    }
                }

              
        }
    }
   
}

Time Conversion

Given a time in -hour AM/PM format, convert it to military (-hour) time.
Note: Midnight is on a -hour clock, and on a -hour clock. Noon is on a -hour clock, and on a -hour clock.
Input Format
A single string containing a time in -hour clock format (i.e.: or ), where and .
Output Format
Convert and print the given time in -hour format, where .


Solution :


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String time = in.next();
        try{           
                SimpleDateFormat date12Format = new SimpleDateFormat("hh:mm:ssa");
                SimpleDateFormat date24Format = new SimpleDateFormat("HH:mm:ss");

                System.out.println(date24Format.format(date12Format.parse(time)));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}