Friday, March 24, 2017

Sum Of Subset of an Array

Program to find sum of subset of a given array and match it with the given sum, if its match with the sum it will print "Yes", if not match with the given sum print "No" if sum of subset is negative number print "error".

Exp :
1
4 8
1
3
5
3

1 is the number of test case, 4 is size of array , 8 is the given sum , 
output : Yes


Sum Of Subset of an Array

Thursday, March 23, 2017

Print biggest numbers from the Array

This is the basic program to print biggest number from the array, but if in the right side of the array is big number from our current number skip it from printing ,else in the right no number is bigger then current number so print it.

Ex : 3,5203,3,6,7,8,1,2,23,54,232,554,879,0

in this array we need to start comparing numbers from 3 and we will go till 0, if we found any bigger number then 3 in right side we will skip it and move to next element.

Output : 5203,879,0

it printing 0 also because there is no number in the right side of 0 so its the biggest.

Print biggest numbers from the Array

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]);
                    }
                }

              
        }
    }
   
}