Category Archives: hackerearth

Count Digits

You are given a string S. Count the number of occurrences of all the digits in the string S.

 

 

#include <iostream>
using namespace std;

int main()
{ int a = 0, b = 0, c = 0, d = 0, e = 0,f=0,g=0,h=0,i=0,j=0;
    string s;
    // Read given string from STDIN
    cin >> s;

    int s_len = s.length();
    // Iterate over each character in the string
    for(int k=0; k<s_len; k++) {

        // This can be done in a better way using hashing, which simplifies the implementation,
        // however for the purpose of this article we'll restrict the implementation to naive way

        // Check for each character in if else
        if(s[k] == '0') {
            a++;
        } else if(s[k] == '1') {
            b++;
        } else if(s[k] == '2') {
            c++;
        } else if(s[k] == '3') {
            d++;
        } else if(s[k] == '4') {
            e++;
        }
        else if(s[k] == '5') {
            f++;
        }
        else if(s[k] == '6') {
            g++;
        }
        else if(s[k] == '7') {
            h++;
        }
        else if(s[k] == '8') {
            i++;
        }
        else if(s[k] == '9') {
            j++;
        }
    }
    // Print out the result to STDOUT
    cout << "0 " << a << endl;
    cout << "1 " << b << endl;
    cout << "2 " << c << endl;
    cout << "3 " << d << endl;
    cout << "4 " << e << endl;
    cout << "5 " << f << endl;
    cout << "6 " << g << endl;
    cout << "7 " << h << endl;
    cout << "8 " << i << endl;
    cout << "9 " << j << endl;
    
    return 0;
}
Advertisements

Life, the Universe, and Everything

Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. More precisely… rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits.

//import for Scanner and other utility classes*/
import java.util.*;

class TestClass {
public static void main(String args[] ) throws Exception {
/*
* Read input from stdin and provide input before running
* Use either of these methods for input

//BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
int N = Integer.parseInt(line);*/

//Scanner
Scanner s = new Scanner(System.in);

while(s.hasNextInt()){
int n=s.nextInt();
if(n==42)
break;
else
System.out.println(n);
}

}
}

Roy and Profile Picture

Roy wants to change his profile picture on Facebook. Now Facebook has some restriction over the dimension of picture that we can upload.
Minimum dimension of the picture can be L x L, where L is the length of the side of square.

Now Roy has N photos of various dimensions.
Dimension of a photo is denoted as W x H
where W – width of the photo and H – Height of the photo

When any photo is uploaded following events may occur:

[1] If any of the width or height is less than L, user is prompted to upload another one. Print “UPLOAD ANOTHER” in this case.
[2] If width and height, both are large enough and
(a) if the photo is already square then it is accepted. Print “ACCEPTED” in this case.
(b) else user is prompted to crop it. Print “CROP IT” in this case.

(quotes are only for clarification)

Given L, N, W and H as input, print appropriate text as output.

Input:
First line contains L.
Second line contains N, number of photos.
Following N lines each contains two space separated integers W and H.

Output:
Print appropriate text for each photo in a new line.

Constraints:
1 <= L,W,H <= 10000
1 <= N <= 1000

/* IMPORTANT: Multiple classes and nested static classes are supported */

/*
 * uncomment this if you want to read input.
//imports for BufferedReader
import java.io.BufferedReader;
import java.io.InputStreamReader;*/

//import for Scanner and other utility classes
import java.util.*;


class TestClass {
    public static void main(String args[] ) throws Exception {
        /*
         * Read input from stdin and provide input before running
         * Use either of these methods for input

        //BufferedReader
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        int N = Integer.parseInt(line);*/

        //Scanner
        Scanner s = new Scanner(System.in);
        int L = s.nextInt();
        int N = s.nextInt();

        for (int i = 0; i < N; i++) {
            int W=s.nextInt();
            int H=s.nextInt();
        
        if(W<L || H<L){
           System.out.println("UPLOAD ANOTHER"); 
        }
        else if((L <= W)&&(L <= H)){
            if(W == H){
System.out.println("ACCEPTED");
}
else {

        System.out.println("CROP IT");
    }
    }
    }
    }
}

Count Divisors

You have been given 3 integers l, r and k. Find how many numbers between l and r
(both inclusive) are divisible by k. You do not need to print these numbers, you just have to find their count.

Input Format
The first and only line of input contains 3 space separated integers l, r and k.

Output Format
Print the required answer on a single line.

Constraints
1lr1000

1k1000

 


//import for Scanner and other utility classes
import java.util.*;

class TestClass {
    public static void main(String args[] ) throws Exception {
     
        //Scanner
        Scanner s = new Scanner(System.in);
        int l = s.nextInt();
        int r = s.nextInt();
        int k = s.nextInt();
        int count=0;

        for (int i = l; i <=r; i++) {
            if(i%k==0){
                count++;
            }
        }


        System.out.println(count);
    }
}

Find Product

You have been given an array A of size N consisting of positive integers. You need to find and print the product of all the number in this array Modulo 10^9+7

.

Input Format:
The first line contains a single integer N

denoting the size of the array. The next line contains N

space separated integers denoting the elements of the array

Output Format:
Print a single integer denoting the product of all the elements of the array Modulo 10^9+7

.

Constraints:
1N10^3
1A[i]10^3

 

import java.util.Scanner;

public class FindProduct {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
    long arr[] = new long[n];

    long M = 1000000007;
    long answer = 1;

    for (int i = 0; i <n ; i++){
arr[i] = sc.nextLong();
answer = (answer * arr[i]) % M;

}
System.out.println(answer);
}

}