Thursday 25 June 2015

Urban Ladder Coding Challange Sample Questions Asked on 26th June 2015

Q1)FizzBuzz
---------------------------------------------------------------------------------

Write a program that prints the numbers in the given range. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”. Print a new line after each string or number.
Input Format First line will be the number of testcases, T. Next line will have T integers, denoted by N.
Output Format For each testcase, print the number from 1 to N. But follow the rules given in the problem statement.
Constraints
1 <= T <= 10
N is an integer.
Please read the below instructions carefully
  1. You can choose any language from the given list to write your solution.
  2. All input to the programming solution is to STDIN and output is to STDOUT.
  3. You don't have to manually give the input to your program, just take the input from STDIN and the code evaluation engine will provide the input to your program.
  4. For example if you are coding in C, and the first input is an integer then simply do scanf('%d', &i) assuming you are reading that integer to a variable named i. Similarly if you are using C++ simply do cin >> i
  5. There are two different type of test cases. First type is the sample input and output for which you know both the input and output. You can look at them under the problem statement.
  6. When you click Compile and Test the code will be compiled and tested only on the sample input that is shown to you. Compile and Test is for you to understand if you solution is compiling and running against the sample input. 5 When you click submit, your code will be judged on multiple test cases which are hidden. These tests are not available for you to see them so you will not know the input on which your code will be tested. But it is assured that all inputs will be in the given constraint limit and in the given format as stated in the problem statement.
  7. In order for your code to get accepted, it must clear all the judge test cases. In cases where partial marking is allowed, you will awarded partial marks for the number of test cases your solution passes.
  8. Please note that getting green mark when you hit 'Compile and Test' does not indicate anything on the correctness of your program. It just indicates that your code correctly compiled and ran successfully against the sample input. It can still fail for other test inputs that visible to you.
  9. Do not output anything, except what it is asked for in the output section. Note that you have to output only in the way that is mentioned. Any extra strings in the output will be treated as wrong answer. Even an extra space can lead to the answer not being accepted.
  10. Don't assume any constraints on the input based on the sample input that you see, the actual test cases will be much larger in size. But they will always be within the constraints mentioned in the problem.
  11. To understand how the code is evaluated visit the judge page. There is also a sample code in each language there.
To further understand how the judge works, look at one of the actual test input file and the corresponding expected output for this problem


Sample Input                Sample Output   

2
3 15               1
                   2
                   Fizz
                   1
                   2
                   Fizz
                   4
                   Buzz
                   Fizz
                   7
                   8
                   Fizz
                   Buzz
                   11
                   Fizz
                   13
                   14
                   FizzBuzz



Answer
---------------------------------------------------------------------------------

import java.io.BufferedReader;
import java.io.InputStreamReader;


class TestClass {
    public static void main(String args[] ) throws Exception {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        int N = Integer.parseInt(line);
        line=br.readLine();
        String str[]=new String[N];
        int arr[]=new int[N];
        str=line.split(" ");
        for(int l=0;l<str.length;l++){
        arr[l]=Integer.parseInt(str[l]);
        }
        int l=0;
        
        for (int i = 0; i < N; i++) {        
        for(int k=1;k<=arr[l];k++){  
            if(k%3==0 && k%5==0){
        System.out.println("FizzBuzz");
            }else if(k%3==0){
        System.out.println("Fizz");
        }else if(k%5==0){
        System.out.println("Buzz");
        }else{
        System.out.println(k);
        }
       
        }
        l++;
            
        }


       
    }
}



Q2)Sum of Two Numbers
---------------------------------------------------------------------------------

You are given two numbers a and b. Your task is to find the sum of two numbers.
Input
The first line contains an integer t, denoting the number of test cases. Next t lines contain two integers, a and b separated by a space.
Input Constraint
1 <= t <= 1000000000
1 <= a, b <= 1000000000
Output
For every number a and b, output the sum of two numbers a and b.


Sample Input                Sample Output

1
1 2                3




Answer
---------------------------------------------------------------------------------

import java.math.BigInteger; 
import java.io.BufferedReader;
import java.io.InputStreamReader;

class TestClass {
    public static void main(String args[] ) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        int N = Integer.parseInt(line);
        for (int i = 0; i < N; i++) {
         line=br.readLine();
         String str[]=new String[line.length()];
      str=line.split(" ");
         BigInteger a=new BigInteger(str[0]);
   BigInteger b=new BigInteger(str[1]);
   BigInteger c=new BigInteger("0");
   c=b.add(a);
   System.out.println(c);
        }
    }
}




HackerEarth Questions Asked on 24th June 2015

Q)Monk and the Magical Candy Bags
---------------------------------------------------------------------------------

Our Monk loves candy!
While taking a stroll in the park, he stumbled upon N Bags with candies. The i'th of these bags contains Ai candies.
He picks up a bag, eats all the candies in it and drops it on the ground. But as soon as he drops the bag, the number of candies in the bag increases magically! Say the bag that used to contain X candies (before eating), now contains [X/2] candies! ,where [x] is the greatest integer less than x (Greatest Integer Function).
Amazed by the magical spell, Monk can now have a lot more candies! But he has to return home in K minutes. In a singleminute,Monk can consume all the candies in a single bag, regardless of the number of candies in it.
Find the maximum number of candies that Monk can consume.
Input:
First line contains an integer TT test cases follow.
First line of each test case contains two space-separated integers N and K.
Second line of each test case contains N space-separated integers,the number of candies in the bags.
Output:
Print the answer to each test case in a new line.

Constraints:
1 ≤ T ≤ 10
1 ≤ N ≤ 105
0 ≤ K ≤ 105
0 ≤ Ai ≤ 1010

Sample Input                       Sample Output
1
5 3
2 1 7 4 2               14





Answer
---------------------------------------------------------------------------------

import java.io.BufferedReader;
import java.io.InputStreamReader;


class TestClass {
    public static void main(String args[] ) throws Exception {

     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       String line = br.readLine();
       int N = Integer.parseInt(line);
       int monk=0;
       for (int i = 0; i < N; i++) {
        line=br.readLine();
        String str[]=new String[2];
        str=line.split(" ");
        int a=Integer.parseInt(str[0]);
        int b=Integer.parseInt(str[1]);        
        int arr[]=new int[a];
       
        line=br.readLine();
        String st1[]=new String[a];
        st1=line.split(" ");
        for(int l=0;l<st1.length;l++){
        arr[l]=Integer.parseInt(st1[l]);
        }
           for(int j=0;j<b;j++){
            long max=0;
              int d=0;
for(int k=0;k<arr.length-1;k++){
if(arr[k]>arr[k+1]&&arr[k]>max){
max=arr[k];
d=k;
}else if(arr[k]<arr[k+1]&&arr[k+1]>max){
max=(long)arr[k+1];
d=k+1;
}
}
arr[d]/=2;
monk+=max;
}  
           }
       System.out.print(monk);
           
        
       }
}




Wednesday 24 June 2015

HackerEarth Questions Asked on 10th Nov 2012

Q1) Small Factorials
---------------------------------------------------------------------------------


You are asked to calculate factorials of some small positive integers.
Input
An integer T, denoting the number of testcases, followed by T lines, each containing a single integer N.
Output
For each integer N given at input, output a single line the value of N!
Input Constraint
1 <= T <= 100
1 <= N <= 100

Input                 Output
4
1                      1
2                      2
5                      120
3                      6
1000000           Output will be larger than int and long so you have to take Array or                             BigInteger type datatype




Answer
---------------------------------------------------------------------------------

import java.math.BigInteger;
import java.io.BufferedReader;
import java.io.InputStreamReader;

class TestClass {
    public static void main(String args[] ) throws Exception {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        int N = Integer.parseInt(line);
        for (int i = 0; i < N; i++) {
            line=br.readLine();
          long g=Long.parseLong(line);
          BigInteger d=new BigInteger("1");
          BigInteger c=new BigInteger("1");
        BigInteger b=new BigInteger("1");
        for(int f=1;f<=g;f++){
        b=b.multiply(c);
        d=new BigInteger("1");
        c=c.add(d);
              }
        System.out.println(b.toString());

        }
        
    }
}





Q) What is the string made of? 
---------------------------------------------------------------------------------

You are given a string, which contains entirely of decimal digits (0-9). Each digit is made of a certain number of dashes, as shown in the image below. For instance 1 is made of 2 dashes, 8 is made of 7 dashes and so on.




You have to write a function that takes this string message as an input and returns a corresponding value in terms of a number. This number is the count of dashes in the string message.
Note:
0 consists of 6 dashes, 1 consists of 2 dashes, 2 consists of 5 dashes, 3 consists of 5 dashes, 4 consists of 4 dashes, 5 consists of 5 dashes, 6 consists of 6 dashes, 7 consists of 3 dashes [though the figure shows that 7 consists of 4 dashes but due to minor mistake in the problem please write your solution assuming 7 consists of 3 dashes], 8 consists of 7 dashes, 9 consists of 6 dashes.
Constraints
  • String message will contain at least one digit, but not more than 100
  • Each character in code will be a digit ('0'-'9').


Input                Output
12134        18








Answer
---------------------------------------------------------------------------------


import java.io.BufferedReader;
import java.io.InputStreamReader;
class TestClass {
    public static void main(String args[] ) throws Exception {
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        String st=line;
        int count=0;
        for (int i=0; i<st.length(); i++) {
         if(st.charAt(i)=='1'){
          count=count+2;
         }else if(st.charAt(i)=='2'){
          count=count+5;
         }else if(st.charAt(i)=='3'){
          count=count+5;
         }else if(st.charAt(i)=='4'){
          count=count+4;
         }else if(st.charAt(i)=='5'){
          count=count+5;
         }else if(st.charAt(i)=='6'){
          count=count+6;
         }else if(st.charAt(i)=='7'){
          count=count+3;
         }else if(st.charAt(i)=='8'){
          count=count+7;
         }else if(st.charAt(i)=='9'){
          count=count+6;
         }else if(st.charAt(i)=='0'){
          count=count+6;
         }            
        }


        System.out.println(count);
    }
}

Monday 22 June 2015

HackerEarth Questions Asked on 9th June 2015


Q1) Chandu and his Girlfriend
---------------------------------------------------------------------------------


Chandu's girlfriend loves arrays that are sorted in non-increasing order. Today is her birthday. Chandu wants to give her some sorted arrays on her birthday. But the shop has only unsorted arrays. So, Chandu bought T unsorted arrays and is trying to sort them. But, he doesn't have much time to sort the arrays manually as he is getting late for the birthday party. So, he asked you to write a program to sort the T arrays in non-increasing order. Help him, or his girlfriend will kill him.
Input:
First line contains an integer T, denoting the number of test cases.
First line of each test case contains an integer N, denoting the size of the array.
Second line contains N space separated integers, denoting the array elements Ai.
Output:
For each test case, print the sorted array in non-increasing order.

Constraints:
1 <= T <= 100
1 <= N <= 105
0 <= Ai <= 109

Sample Input               Sample Output
2
5
2 5 2 4 3          5 4 3 2 2
5
5 4 2 3 1          5 4 3 2 1


Answer
---------------------------------------------------------------------------------

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Random;
public class Chandu_And_His_Girlfriend {
   public static void main(String args[] ) throws Exception {  
         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         String line = br.readLine();
         int N = Integer.parseInt(line);
         for (int i = 0; i < N; i++) {
          line=br.readLine();
          int k=Integer.parseInt(line);
          int arr[]=new int[k];
          line=br.readLine();
          String str[]=line.split(" ");
           for(int p=0;p<str.length;p++){
            arr[p]=Integer.parseInt(str[p]);
           }
          int[] ser=quickSort(arr, 0,arr.length-1);
          for(int d=0;d<ser.length;d++){
           System.out.print(ser[d]+" ");
          }    System.out.println();
          }
      }

  public  static int partition(int[] arr1,int start,int end) {
   int pivot = arr1[end];
   int partitionIndex=start;
   for(int i=start;i<=end;i++){
    if(arr1[i]>pivot){
     int tmp=arr1[i];
     arr1[i]=arr1[partitionIndex];
     arr1[partitionIndex]=tmp;
     partitionIndex++;
    }
   }
   int tmp=arr1[partitionIndex];
   arr1[partitionIndex]=arr1[end];
   arr1[end]=tmp;
   return partitionIndex;
  }
  public static int[] quickSort(int[] arr1,int start,int end){
   if(start<end){
    int partitionIndex=partition(arr1, start, end);
    quickSort(arr1, start, partitionIndex-1);
    quickSort(arr1, partitionIndex+1, end);
   }
   return arr1;
  }
}

Sunday 21 June 2015

HackerEarth Questions Asked on 4th June 2015


Q1) Terrible Chandu
---------------------------------------------------------------------------------

Chandu is a bad student. Once his teacher asked him to print the reverse of a given string. He took three hours to solve it. The teacher got agitated at Chandu and asked you the same question. Can you solve it?
Input:
The first line contains an integer T, denoting the number of test cases.
Each test case contains a string S, comprising of only lower case letters.
Output:
For each test case, print the reverse of the string S.
Constraints:
1 <= T <= 10
1 <= |S| <= 30


Sample Input                 Sample Output
2
ab                              ba
aba                             aba



Answer
----------

import java.io.BufferedReader;
import java.io.InputStreamReader;


class TestClass {
         public static void main(String args[] ) throws Exception {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String line = br.readLine();
            int N = Integer.parseInt(line);
            for (int i = 0; i < N; i++) {
                String s=br.readLine();
                String r="";
                for(int j=s.length()-1;j>=0;j--){
                   r=r+s.charAt(j);
                }
               System.out.println(r);
           }
       }
}



Q2) Chandu and Consecutive Letters
---------------------------------------------------------------------------------
Chandu is very fond of strings. (Or so he thinks!) But, he does not like strings which have same consecutive letters. No one has any idea why it is so. He calls these strings as Bad strings. So, Good strings are the strings which do not have same consecutive letters. Now, the problem is quite simple. Given a string S, you need to convert it into a Good String.
You simply need to perform one operation - if there are two same consecutive letters, delete one of them.
Input:
The first line contains an integer T, denoting the number of test cases.
Each test case consists of a string S, which consists of only lower case letters.
Output:
For each test case, print the answer to the given problem.
Constraints:
1 <= T <= 10
1 <= |S| <= 30

Sample Input           Sample Output
3
abb            ab
aaab                     ab
ababa                   ababa




Answer
----------

import java.io.BufferedReader;
import java.io.InputStreamReader;


class TestClass {
    public static void main(String args[] ) throws Exception {
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        int N = Integer.parseInt(line);
        for (int i = 0; i < N; i++) {
        String s= br.readLine();
        String r="";
        int x;
        for(int j=0;j<s.length();j=x){
        x=j;
        while(++x<s.length() && s.charAt(x)==s.charAt(j));
        r=r+s.charAt(j);
        }
            System.out.println(r);
        }
    }
}