Looping and Skipping

Problem Statement

for loops in Bash can be used in several ways:
– iterating between two integers, a and b
– iterating between two integers, a and b, and incrementing by c each time
– iterating through the elements of an array, etc.

Your task is to use for loops to display only odd natural numbers from 1 to 99.

Input
There is no input.

Output

1
3
5
.
.
.
.
.
99  

Recommended Resources

A quick but useful tutorial for bash newcomers is here.
Handling input is documented and explained quite well on this page.
Different ways in which for loops may be used are explained with examples here.

Solution

  
for (( c=1; c<=100; c+=2 ))
do
   echo "$c"
done

Let’s Echo

Problem Statement

Write a bash script which does just one thing: saying “HELLO”.

Input Format
There is no input file required for this problem.

Output Format
HELLO

Sample Output
HELLO

A quick introduction to ‘Echo’
This is the equivalent of common output commands in most programming language (print or puts statements).

For Example:

echo "Greetings"

This outputs just one word “Greetings” (without the quotation marks).

echo "Greetings $USER, your current working directory is $PWD"

This picks up the values of the environment variables $USER and $PWD and displays something like:

Greetings prashantb1984, your current working directory is /home/prashantb1984  

The above message, of course, will vary from system to system, depending on the setting of environment variables.

Recommended Resource
A quick but useful tutorial for bash newcomers is here.

Solution

  
echo "HELLO"

Solve me first FP

Problem Statement

This is an introductory challenge. The purpose of this challenge is to give you a working I/O template in your preferred language. It includes scanning 2 integers from STDIN, calling a function, returning a value, and printing it to STDOUT.

Your task is to scan two numbers from STDIN, and print the sum A+B on STDOUT.

Note: The code has been saved in a template, which you can submit if you want.

Input Format
Given A and B on two different lines.

Output Format
An integer that denotes Sum (A + B)

Constraints
1 ≤ A, B ≤ 1000

Sample Input

2
3

Sample Output

5

Solution : In Haskell

  
solveMeFirst a b = a + b

main = do
    val1 <- readLn
    val2 <- readLn
    let sum = solveMeFirst val1 val2
    print sum


Basic Data Types

Problem Statement

C++ has the following data types along with their format specifier:

  • Int (“%d”): 32 Bit integer
  • Long (%ld): 32 bit integer (same as Int for modern systems)
  • Long Long (“%lld”): 64 bit integer
  • Char (“%c”): Character type
  • Float (“%f”): 32 bit real value
  • Double (“%lf”): 64 bit real value

Reading
In order to read a data type, you need the following syntax:

scanf("`format_specifier`", &val)

E.g., in order to read a character and then a double

char ch;
double d;
scanf("%c %lf", &ch, &d);

P.S.: For the moment, we can ignore the spacing between format specifiers.


Printing
In order to print a data type, you need the following syntax:

printf("`format_specifier`", val)

E.g., in order to print a character and then a double

char ch = 'd';
double d = 234.432;
printf("%c %lf", ch, d);

P.S.: For the moment, we can ignore the spacing between format specifiers.

Input Format

Input will consists of an int, long, long long, char, float and double, each separated by a space.

Output Format

Print the elements in the same order, but each in a new line.

Sample Input

3 444 12345678912345 a 334.23 14049.30493

Sample Output

3
444
12345678912345
a
334.23
14049.30493

Solution

  
#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    // Complete the code.
    int a;
    long b;
    char ch;
    long long c;
    float d;
    double f;    
    scanf("%d %ld %lld %c %f %lf  ", &a,&b,&c,&ch,&d,&f);
    printf("%d\n%ld\n%lld\n%c\n%f\n%lf",a,b,c,ch,d,f);
    return 0;
}


Find Digits

Problem Statement

You are given an integer N. Find the digits in this number that exactly divide N (division that leaves 0 as remainder) and display their count. For N=24, there are 2 digits (2 & 4). Both of these digits exactly divide 24. So our answer is 2.

Note

  • If the same number is repeated twice at different positions, it should be counted twice, e.g., For N=122, 2 divides 122 exactly and occurs at ones’ and tens’ position. So for this case, our answer is 3.
  • Division by 0 is undefined.

Input Format

The first line contains T (the number of test cases), followed by T lines (each containing an integer N).

Constraints
1T15
0<N<1010

Output Format

For each test case, display the count of digits in N that exactly divide N in a separate line.

Sample Input

2
12
1012

Sample Output

2
3

Explanation

2 digits in the number 12 divide it exactly. The first digit, 1, divides it exactly in twelve parts, and the second digit, 2 divides 12 equally in six parts.

1 divides 1012 exactly (and it occurs twice), and 2 also divides it exactly. Division by 0 is undefined, therefore it will not be counted.

This challenge was part of Pragyan 12.

Solution

  
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
    vector vec;
    int t,n,a,count;
    cin>>t;
    while(t--)
        {
        count = 0;
        cin>>n;
        a=n;
        while(n!=0)
            {
            int temp = n%10;
            vec.push_back(temp);
            n/=10;
            }
        reverse(vec.begin(),vec.end());
        for(std::vector::iterator it = vec.begin(); it != vec.end(); ++it)
            {
                if(*it==0);
                else if((a%*it)==0)
                   count++;
                   
            }
        cout<<count<<endl;
        vec.clear();
    }
    return 0;
}


Utopian Tree

Problem Statement

The Utopian Tree goes through 2 cycles of growth every year. The first growth cycle occurs during the spring, when it doubles in height. The second growth cycle occurs during the summer, when its height increases by 1 meter.

Now, a new Utopian Tree sapling is planted at the onset of spring. Its height is 1 meter. Can you find the height of the tree after N growth cycles?

Input Format

The first line contains an integer, T, the number of test cases.
T lines follow; each line contains an integer, N, that denotes the number of cycles for that test case.

Constraints
1T10
0N60

Output Format

For each test case, print the height of the Utopian Tree after N cycles. Each line thus has to contain a single integer, only.

Sample Input

3
0
1
4

Sample Output

1
2
7

Explanation

There are 3 test cases.

In the first case (N=0), the initial height (1) of the tree remains unchanged.

In the second case (when N = 1, i.e. after the 1st cycle), the tree doubles its height as it’s planted at the onset of spring.

In the third case (N=4), the tree first doubles its height (2), then grows a meter (3), then doubles again (6), before growing another meter; at the end of the 4th cycle, its height is 7 meters.

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)throws Exception {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int i,j,yr;
        int t = Integer.parseInt(br.readLine());
        while(t!=0){
            yr=1;
            int n=  Integer.parseInt(br.readLine());
            for(i=1;i<=n;i++){
                if(i%2==0)
                    yr++;
                else yr= yr *2;
            }
            System.out.println(yr);
            t--;
        }

    }
}

Sherlock and The Beast

Problem Statement

Sherlock Holmes is getting paranoid about Professor Moriarty, his arch-enemy. All his efforts to subdue Moriarty have been in vain. These days Sherlock is working on a problem with Dr. Watson. Watson mentioned that the CIA has been facing weird problems with their supercomputer, ‘The Beast’, recently.

This afternoon, Sherlock received a note from Moriarty, saying that he has infected ‘The Beast’ with a virus. Moreover, the note had the number N printed on it. After doing some calculations, Sherlock figured out that the key to remove the virus is the largest Decent Number having N digits.

A Decent Number has the following properties:

  1. 3, 5, or both as its digits. No other digit is allowed.
  2. Number of times 3 appears is divisible by 5.
  3. Number of times 5 appears is divisible by 3.

Meanwhile, the counter to the destruction of ‘The Beast’ is running very fast. Can you save ‘The Beast’, and find the key before Sherlock?

Input Format
The 1st line will contain an integer T, the number of test cases. This is followed by T lines, each containing an integer N. i.e. the number of digits in the number.

Output Format
Largest Decent Number having N digits. If no such number exists, tell Sherlock that he is wrong and print 1.

Constraints
1T20
1N100000

Sample Input

4
1
3
5
11

Sample Output

-1
555
33333
55555533333

Explanation
For N=1, there is no such number.
For N=3, 555 is the only possible number.
For N=5, 33333 is the only possible number.
For N=11, 55555533333 and all permutations of these digits are valid numbers; among them, the given number is the largest one.

Solution

  
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int t,n;
    cin>>t;
    while(t--)
    {
       cin>>n;
       string ks;
       for(int j=n;j>=0;j--)
       {
           if(j%3==0 && (n-j)%5==0)
           {
            ks="";
            for(int i=0;i<j;i++)
                ks+='5';
            for(int i=0;i<n-j;i++)
                ks+='3';
            break;   
            }
       }
       if(ks=="")
               cout<<-1<<endl;
       else 
               cout<<ks<<endl;
    }
    return 0;
}


Angry Professor

Problem Statement

The professor is conducting a course on Discrete Mathematics to a class of N students. He is angry at the lack of their discipline, and he decides to cancel the class if there are fewer than K students present after the class starts.

Given the arrival time of each student, your task is to find out if the class gets cancelled or not.

Input Format

The first line of the input contains T, the number of test cases. Each test case contains two lines.
The first line of each test case contains two space-separated integers, N and K.
The next line contains N space-separated integers, a1,a2,,aN, representing the arrival time of each student.

If the arrival time of a given student is a non-positive integer (ai0), then the student enters before the class starts. If the arrival time of a given student is a positive integer (ai>0), the student enters after the class has started.

Output Format

For each testcase, print “YES” (without quotes) if the class gets cancelled and “NO” (without quotes) otherwise.

Constraints

  • 1T10
  • 1N1000
  • 1KN
  • 100ai100,where i[1,N]

Note
If a student enters the class exactly when it starts (ai=0), the student is considered to have entered before the class has started.

Sample Input

2
4 3
-1 -3 4 2
4 2
0 -1 2 1

Sample Output

YES
NO

Explanation

For the first test case, K=3, i.e., the professor wants at least 3 students to be in class but there are only 2 who have arrived on time (3 and 1), hence the class gets cancelled.

For the second test case, K=2, i.e, the professor wants at least 2 students to be in class and there are 2 who have arrived on time (0 and 1), hence the class does not get cancelled.

Solution

  
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int t,n,k,i,count;
    cin>>t;
    while(t--){
        count=0;
        cin>>n>>k;
        for(i = 0; i < n; i++)
            { 
            int temp;
            cin>>temp;
            if(temp <= 0)
                count++;
        }
        if(count >= k)
            cout<<"NO"<<endl;
        else cout<<"YES"<<endl;
    }
    return 0;
}


Extra long factorials

Problem Statement

You are given an integer N. Print the factorial of this number.

N!=N×(N1)×(N2)××3×2×1

Note: Factorials of N>20 can’t be stored even in a 64bit long long variable. Big integers must be used for such calculations. Languages like Java, Python, Ruby etc. can handle big integers but we need to write additional code in C/C++ to handle such large values.

We recommend solving this challenge using BigIntegers.

Input Format
Input consists of a single integer N.

Constraints
1N100

Output Format
Output the factorial of N.

Sample Input

25

Sample Output

15511210043330985984000000

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)  throws Exception{
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        //BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
        //int n = Integer.parseInt(br.read());
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        BigInteger ans = BigInteger.ONE;
        for (int i=1;i<=n;i++)
            {
            ans = ans.multiply(new BigInteger(String.valueOf(i))); 
        }
        System.out.println(ans);
    }
}