Sunday, August 6, 2017

Triangle Pattern

Given an odd integer N, print a triangle with * as mentioned in the below examples.

Input Format:
The first line contains N.

Output Format:
N/2 + 1 lines containing the triangle pattern as shown in the example input/output.

Boundary Conditions:
1 <= N <= 999

Example Input/Output 1:
Input:
5

Output:
!!*!!
!***!
*****

Example Input/Output 2:
Input:
9

Output:
!!!!*!!!!
!!!***!!!
!!*****!!
!*******!
*********


Solution-1:
n = int(input())
print('\n'.join(('*'*i).center(n,'!')for i in range(1,n+1,2)))

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

Wednesday, August 2, 2017

Multi Layered Diamond Shaped Rhombus Pattern

Given an odd value of N, the program must print multi layered rhombus pattern in diamond shapes whose side contains N, N-2, ... 1 slashes respectively as shown below in the examples.
Input Format:
The first line contains N.
Output Format:
The multi layered rhombus pattern in diamond shapes whose side contains N, N-2, ... 1 slashes respectively. Hash symbol is used as filler for other values.
Boundary Conditions:
1 <= N <= 101 and N is odd.
Example Input/Output:
Input:
5
Output:
####/\####
###/##\###
##/#/\#\##
#/#/##\#\#
/#/#/\#\#\
\#\#\/#/#/
#\#\##/#/#
##\#\/#/##
###\##/###
####\/####
Solution:
n = int(input())
for i in range(2*n):
    for j in range(2*n):
        if i<n and j<n and j>=n-i-1 and (j-n+1+i)%2==0:
            print('/',end='')
        elif i<n and j>=n and j<=n+i and (j-n-i)%2==0:
            print('\\',end='')
        elif i>=n and j<n and j>=i-n and (j-i+n)%2==0:
            print('\\',end='')
        elif i>=n and j>=n and j<=3*n-i-1 and (3*n-i-1-j)%2==0:
            print('/',end='')
        else:
            print('#',end='')
    print()  
Share:

Count Pair Difference

Given N integers, count the number of pairs of integers whose difference is K.

Input Format

The first line contains N and K.
The second line contains N numbers of the set. All the N numbers are unique.

Constraints

  • 2 <= N <= (10^5)
  • 0 < K < (10^9)
  • Each integer will be greater than 0 and at least K smaller than (2^31)-1.

Output Format

An integer that tells the number of pairs of integers whose difference is K .

Sample Input

5 2 
1 5 3 4 2 

Sample Output

3

Explanation

There are 3 pairs of integers in the set with a difference of 2.

Solution:

n,d=[int(i) for i in input().split()]
a = [int(i) for i in input().split()]
s = set(a)
c = 0
for i in a:
    if i+d in s:
        c+=1
print(c)


Share:

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: