Matrix Diagonal Sum
Given a square matrix, print the sum all diagonal elements of the matrix.
Input
The first line of input will be an integer N, denoting the no. of rows in the matrix. The following N lines will contain space-separated integers denoting the elements of each row of the matrix.
Output
The output should be a single integer denoting the sum of all the diagonal elements.
Explanation
For example, if the given N is 3, and the given matrix is
1 2 3
4 5 6
7 8 9
The diagonal and anti-diagonal elements of the above matrix are 1, 3, 5, 7, 9. So the output should be 25
Sample Input
3
1 2 3
4 5 6
7 8 9
Sample Output
25
Repeating & Non-Repeating Numbers
Given a list of numbers, find and print, the first number that occurs only once in the list and the first number that occurs more than once in the list.
Input
The input will be a single line containing space-separated integers.
Output
The first line of the output should contain the first non-repeating number. The second line of output should contain first repeating number. If there is no number fitting the given criteria, print "None" instead.
Explanation
For example, if the given list of integers are 5, 5, 4, 7, 4, 1, 11 , the first non-repeating number is 7 and the first repeating number is 5.
Sample Input 1
5 5 4 7 4 1 11
Sample Output 1
7
5
Sample Input 2
1 2 3 4 5 6 7 8 9
Sample Output 2
1
None
Area of Largest Rectangle in Histogram
Given an list of integer
heights representing the histogram's bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram.
Input
The input will be a single line containing space-separated integers, denoting the heights of the bars.
Output
The output should be a single line containing the area of the largest rectangle in the rectangle.
Explanation
For example, if the given list of numbers are
2 1 5 6 2 3 , when they are drawn adjacent to each other according to their heights, the histogram will be like
The largest rectangle can be found in between 5 and 6, which is 10.
Sample Input 1
2 1 5 6 2 3
Sample Output 1
10
Sample Input 2
65 87 96 31 32 86 57 69 20 42
Sample Output 2
248
Palindrome Pairs
Given a list of unique words, write a program to print all the pairs of the distinct indices (i, j) in the given list, so that the concatenation of two words at indices i and j is a palindrome.
Input
The input will be a single line containing a sentence.
Output
The output should be printing each distinct pair of indices (i, j) each in a line in ascending order.
If there are no distinct pairs found, print "-1"
Note: Consider (i, j) and (j, i) as two different pairs.
Explanation
For example, if the given sentence is
was it a car or a cat I saw
The words that can be concatenated to make a palindrome are
Words Indices
(was, saw) (0, 8)
(a, a) (2, 5)
(a, a) (5, 2)
(saw, was) (8, 0)
So the output should be
0 8
2 5
5 2
8 0
Sample Input 1
was it a car or a cat I saw
Sample Output 1
0 8
2 5
5 2
8 0
Sample Input 2
bat tac tab cat
Sample Output 2
0 2
1 3
2 0
3 1
Given a 2D (2 dimensional) array, find the largest element in it.
Write a function solution that accepts a 2D-array A and two integers M (number of rows) and N (number of columns). The function should return the largest element in an array.
Input
2
3
1 4 5
2 3 0
Output
5n, m = input('n * m ').split()
n, m = int(n), int(m)
M = []
for i in range(n):
tmp = list(int(el) for el in input().split())
if len(tmp) != m:
raise ValueError
M.append(tmp)
for i in range(n):
for j in range(m):
if i == j or i == m-j-1:
continue
else:
M[i][j] = 0
for row in M:
print(row, sep=' ')
output should not be like this:
[4, 0, 0, 0, 4]
[0, 4, 0, 7, 0]
[0, 0, 8, 0, 0]
[0, 6, 0, 2, 0]
[3, 0, 0, 0, 3]
output should be like this without square brackets and coma:
4 0 0 0 4
0 4 0 7 0
0 0 8 0 0
0 6 0 2 0
3 0 0 0 3
You are given a positive integer N. Your task is to find the number of positive integers K <= N such that K is not divisible by any of the following numbers 2, 3, 4, 5, 6, 7, 8, 9, 10.
In the given example,
11 is not divisible by any number from 2 to 10. It is divisible by only 1, 11.So, the output should be
2.
Given a MxN matrix, write a program to replace all elements that do not belong to principal-diagonal & anti-diagonal with Zeros.
Principal-diagonal elements are the set of elements of a matrix that lie on the line joining the top left corner to the bottom right corner.
Anti-diagonal elements are the set of elements of a matrix that lie on the line joining the bottom left corner to the top right corner.Input
The first line of input will contain two space-separated integers, denoting MxN matrix.
The next M lines will contain N space-separated integers.Output
The output should be MxN matrix by replacing elements that do not belong to principal-diagonal and anti-diagonal with Zeros. Print each row as a space separated integers in a single line.
input:5 5
4 3 7 6 4
4 4 7 7 6
9 5 8 5 9
3 6 6 2 4
3 7 4 4 3
output should be like this:4 0 0 0 4
0 4 0 7 0
0 0 8 0 0
0 6 0 2 0
3 0 0 0 3
Given an integer N as a starting number and K as input, write a program to print a number pyramid of K rows as shown below.
In the example, the given starting number is
10, and the number of rows in the pyramid is 5.So, the output should be
24
23 22
21 20 19
18 17 16 15
14 13 12 11 10