Sunday, July 30, 2017

Total Dribble Paths

A football coach wants his team to improve their dribbling skills. So he sets up a R * C grid, where R is the number of rows and C is the number of columns. From any given cell, a player can only dribble to the right cell or bottom cell.
A player always starts from top left most cell and must end at bottom right most cell where he can collect the reward if he has dribbled according to the rules above.
The program must accept R, C and print the total number of possible paths P in which a player can reach the destination.
Input Format:
The first line contains R.
The second line contains C.
Output Format:
The first line contains P.
Boundary Conditions:
1 <= R, C <= 100
Example Input/Output 1:
Input:
2
3
Output:
3
Example Input/Output 2:
Input:
3
3
Output:
6
Example Input/Output 3:
Input:
25
21
Output:
102758710
Solution:
n,m = int(input()),int(input())
c = [[0 for _ in range(m)] for _ in range(n)]
for i in range(n):
    c[i][0] = 1
for j in range(m):
    c[0][j] = 1
for i in range(1,n):
    for j in range(1,m):
        c[i][j] = c[i-1][j]+c[i][j-1]
print(c[n-1][m-1])
Share:

Wednesday, July 26, 2017

Left Greater Number

Given an array of N positive integers, for each number print the first number to it’s left which is greater than the current number. Print -1 if there is no greater number on it's left.

Input Format:

The first line contains N.
The second line contains N space separated positive integers.

Output Format:

N integers which represent the left greater numbers separated by a space.

Boundary Conditions: 2 <= N <= 10^6

Example Input/Output 1:

Input:
6
5 3 2 4 8 6

Output:
-1 5 3 5 -1 8

Example Input/Output 2:

Input:
11
3 8 5 11 10 10 3 16 11 12 15

Output:
-1 -1 8 -1 11 11 10 -1 16 16 16
 

Solution:

n,a,s=int(input()),[int(x) for x in input().split()],[]
for i in a:
    while s and s[-1] <= i:
        s.pop()
    if not s:
        print('-1',end=' ')
    else:
        print(s[-1],end=' ')
    s.append(i)
Share:

Tuesday, July 25, 2017

2D Matrix - Elements Adjacent Sum

Given a matrix of R rows and C columns, for each element print the sum of the adjacent elements.

Input Format:

The first line contains R The second line contains C Next R lines each contains C values separated by a space

Output Format:

R lines each containing C values which represent the sum of the adjacent elements.

Boundary Conditions: 2 <= R,C <= 50 1 <= Matrix Elements <= 100

Example Input/Output 1:

Input:
5 5
11 11 5 6 18
12 4 16 9 19
5 20 7 5 2
20 19 7 2 11
16 18 5 16 17

Output:
11 16 17 23 6
4 28 13 35 9
20 12 25 9 5
19 27 21 18 2
18 21 34 22 16

Solution:

n,m=int(input()),int(input())
a = [[int(x) for x in input().split()]for _ in range(n)]
for x in a:
    for i in range(len(x)):
        if i == 0:
            print(x[i+1],'',end='')
        elif i == len(x)-1:
            print(x[i-1],'',end='')
        else:
            print(x[i+1]+x[i-1],'',end='')
    print()
Share:

Monday, July 24, 2017

Rhombus Pattern - N Slashes Side


Given an odd value of N, the program must print a rhombus in diamond shape whose side contains N slashes as shown below in the examples.

Input Format:


The first line contains N.

Output Format:


The rhombus in diamond shape with each side containing N slashes. Asterisk is used as a filler for other values.

Boundary Conditions:1 <= N <= 101 and N is odd.

Example Input/Output 1:
Input:
5

Output:
****/\****
***/**\***
**/****\**
*/******\*
/********\
\********/
*\******/*
**\****/**
***\**/***
****\/****
 

Solution - 1:
n = int(input())
for i in range(2*n):
    for j in range(2*n):
        if i<n and j==n-i-1:
            print('/',end='')
        elif i<n and j==n+i:
            print('\\',end='')
        elif i>=n and j==i-n:
            print('\\',end='')
        elif i>=n and j==3*n-i-1:
            print('/',end='')
        else:
            print('*',end='')
    print()

Solution - 2:  

n = int(input())
print('\n'.join(('/'+'**'*i+'\\').center(2*n,'*') if i< n else ('\\'+'**'*(2*n-i-1)+'/').center(2*n,'*') for i in range(2*n)))
Share:

Sunday, July 23, 2017

King, Poet and Gold Coins

A king was happy with the skills of a poet and hence wanted to reward him. The king placed N boxes in a straight line each with certain number of gold coins - C(1) .... C(N) in it. The poet can pick any number of boxes but if the poet selected any box, then he should not pick the boxes which are adjacent to it (either to it's left or right). The poet also cannot rearrange the boxes. Given N and the coins C(i) (where i= 1 to N) in each of the boxes, find the maximum gold coins M, the poet can earn as his reward by picking the boxes.

Input Format:
The first line contains N. The second line contains N positive integers representing the gold coins in each of the boxes 1 to N.

Output Format:
The first line contains M which represents the maximum number of gold coins the poet can earn as reward.

Boundary Conditions: 3 <= N <= 999999 1 <= C(i) <= 100

Example Input/Output 1:
Input:
4
5 3 11 20

Output:
25

Example Input/Output 2:
Input:
7
10 20 15 1 9 12 5

Output:
39

Solution:

n,a = int(input()),[int(x) for x in input().split()]
x,y = a[0],max(a[0],a[1])
for i in range(2,n):
    m = max(a[i]+x,y)
    x = y
    y = m
print(m)
Share:

Max Winning Streak Game Points

Alok is playing N round of card games with his friend Brinda. He gets postive or negative points based on whether he won or lost in a specific round. Alok can also get zero points in case of a tie. Given the points obtained in N rounds, the program must print the maximum sum of points obtained in any of the winning streak sequence.

Input Format:

The first line contains N. The second line contains the points P(i), i=1 to N with the points separated by a space.

Output Format:

The first line contains M which represents the maximum number of points obtained in any of the winning sequence.

Boundary Conditions: 3 <= N <= 100 -100 <= P(i) <= 100

Example Input/Output 1:

Input:
7
9 2 3 -1 1 2 4

Output:
14

Example Input/Output 2:

Input:
15
11 20 6 7 -4 2 -5 15 8 3 16 8 3 -1 -2

Output:
53

Solution:

n = int(input())
a = [int(x) for x in input().split()]
c,s = 0,0
for i in a:
    if i > 0:
        s += i
    else:
        s = 0
    c = max(c,s)

print(c)
Share:

Saturday, July 22, 2017

Numbers with unit Digit

Given two positive integers A and B and a digit U, the program must print all the integers from A to B (inclusive of A and B), which have their unit digit as U.

Input Format:

The first line contains A The second line contains B The third line contains U

Output Format:

The first line contains the integers from A to B having their unit digit as U in ascending order with each integer separated by a space.

Boundary Conditions: 1 <= A,B <= 99999 0 <= U <= 9

Example Input/output 1:

Input:
20
210
3

Output:
23 33 43 53 63 73 83 93 103 113 123 133 143 153 163 173 183 193 203

Solution:

x,y,a = int(input()),int(input()),int(input())


print(*[i for i in range((x//10)*10+a,y+1,10)])
Share:

Wednesday, July 19, 2017

Unique count of Winners

Certain number of people play a card game. They play N rounds. The names of the winner in each round is passed as the input to the program. The program must print the unique count of the winners.

Input Format:

The first line contains the values of N. N lines contain the names of the winners.

Output Format:

The first line contains the unique count of winners.

Boundary Conditions: 1 <= N <= 100 Length of name is from 3 to 100

Example Input/Output 1:

Input:
6
Arun
Bhamini
Arun
Derek
Sahul
Bhamini

Output:
4

Solution:

n = int(input())
s = []
for _ in range(n):
    s.append(input().strip())

print(len(set(s)))
Share:

Dog jumping barricade

Celine is training her dog to participate in a jumping show where the barricades height differs randomly. As the barricades are inclined, if the dog jumps X feet, it will be dragged down by Y feet due to its body weight until it balances and jumps again. Every day she trains her dogs to jump ‘N‘ such barricades. Help Celine by complete the program below which calculates the minimum number of jumps the dog needs to jump the barricades for D days.

Input Format:

The first line contains the value of D, which denotes the number of days the dog is trained. For each day, the first line contains the values of X, Y and N, with the values separated by a space and the second line contains the height of the N barricades separated by a space.

Output Format:

D lines containing the minimum jump required by the dog on each day from 1 to D.

Boundary Conditions: 2 <= D <= 15 1 <= N <= 20 X > Y

Example Input/Output 1:

Input:
2
10 2 4
5 14 19 22
4 1 5
6 9 11 4 6

Output:
9
12

Explanation:
The dog has been trained for 2 days.
First Day: X = 10, Y=2 and N - the number of barricades is 4.
So to jump first barricade of height 5 feet, it needs just 1 jump.
To jump second barricade of height 14 feet, it needs 2 jumps.
To jump third barricade of height 19 feet, it needs 3 jumps. (After first jump it is at 10-2 = 8 feet, second jump it is at 8+10-2 = 16 feet and in third it crossed the barricade).
To jump fourth barricade of height 22 feet, it needs 3 jumps.
Hence a total of 1+2+3+3 = 9 jumps are needed which is the output for Day 1. Similarly for the second day, the number of jumps required is 12.

Solution:

z = int(input())
for _ in range(z):
    j,s,n = [int(x) for x in input().split()]
    a = [int(x) for x in input().split()]
    c = 0
    for i in range(n):
        k = 1
        d = j
        while d < a[i]:
            d = (d-s)+j
            k+=1
        c+=k

    print(c)
Share:

Sum of multiples of X between A and B

Given the values for X, A and B, the program must print the sum of multiples of X from A to B (inclusive of A and B).

Input Format:

The first line contains the value of X. The second line contains the value of A. The third line contains the value of B.

Output Format:

The first line contains the sum of multiples of X from A to B (inclusive of A and B).

Boundary Conditions: 1 <= X <= 100 1 <= A <= 100000 A <= B <= 1000000

Example Input/Output 1:

Input:
5
10
25

Output:
70

Explanation: The multiples of 5 from 10 to 25 are, 10,15,20,25 and their sum is 70

Solution:

n = int(input())
x = int(input())
y = int(input())

print(sum(n*i for i in range((x//n) if x%n==0 else (x//n)+1,(y//n)+1)))
Share:

Students and Department

Given N students name and department, print the X students belonging to a specific department D.

Input Format:

The first line contains the values of N. N lines contain the name and department of N students separated by a space. The next line (N+2)th line, will contain the department name D for which the students list is to be printed.

Output Format:

X lines containing students name where X is the number of students belonging to department D.

Boundary Conditions: 1 <= N <= 100 Length of student name is from 3 to 100 Length of department name is from 3 to 20

Example Input/Output 1:

Input:
5
Arun EEE
Bhuvana ECE
Ganesh MECH
Pavithra ECE
Rohit CSE
ECE

Output:
Bhuvana
Pavithra

Solution - 1:

n = int(input())
a = []
for _ in range(n):
    a.append(input().split())
s = input()
for i in range(n):
    if a[i][1] == s:
        print(a[i][0])



Share:

Numbers Range Count

Given N distinct integers, the program must print the number of ranges R present. A range is defined as two or more consecutive integers.

Input Format:

The first line contains N. The second line contains N integer values separated by a space.

Output Format:

The first line contains R.

Boundary Conditions: 2 <= N <= 100000 1 <= R <= 10000

Example Input/Output 1:

Input:
5
2 1 4 9 3

Output:
1

Explanation:
The only range which is present is 1 2 3 4. 9 is not a range (as a range needs two or more consecutive integers).

Example Input/Output 2:

Input:
7
1 3 11 -15 -20 9 5

Output:
0

Solution:

n = int(input())
a = [int(x) for x in input().split()]
a.sort()
d,c=0,0
for i in range(n-1):
    if a[i]+1 == a[i+1]:
        d +=1
    else:
        d = 0
    if d == 1:
       c += 1

print(c)
Share:

Sunday, July 2, 2017

Decode Ways

A top-secret message string S containing letters from A-Z (only upper-case letters) is encoded to numbers using the following mapping: 'A' -> 1, 'B' -> 2 and so on till Z -> '26' The program has to print the total number of ways in which the received message can be decoded.

Input Format:

The first line contains the string S containing numbers.

Output Format:

The first line contains the number of ways in which S can be decoded.

Boundary Conditions: 1 <= Length of S <= 100

Example Input/Output 1:

Input:

123

Output:

3

Explanation:

1-A 2-B 3-C 12-L 23-W. Hence 123 can be decoded as ABC or AW or LC, that is in 3 ways.

Example Input/Output 2:

Input:

1290

Output:

0

Solution: 

s = input()
l = len(s)
w = [1,0,0]
for i in range(l-1,-1,-1):
    w[2] = w[1]
    w[1] = w[0]
    w[0] = 0
    if i<l and s[i]!='0':
        w[0]+=w[1]
    if i+1<l and (s[i]=='1' or (s[i]=='2' and s[i+1]<'7')):
        w[0]+=w[2]

print(w[0])
Share: