Count Strong Points - Accenture
Count Strong Points - Accenture
Problem:
The program must accept an integer matrix of size RxC as the input. The program must print the number of strong points in the given matrix as the output. The strong points are located at the element(s) whose all the surrounding elements are smaller than the given element.
Input:
3 3
56 92 45
19 41 51
55 31 80
Output:
3
Program:
n,m = map(int,input().split())
l = []
for i in range(n):
l.append(list(map(int,input().split())))
count = 0
for i in range(n):
for j in range(m):
c = 0
if i-1 >= 0 and l[i-1][j] >= l[i][j]:
c = 1
if i+1 < n and l[i+1][j] >= l[i][j]:
c = 1
if j-1 >= 0 and l[i][j-1] >= l[i][j]:
c = 1
if j+1 < m and l[i][j+1] >= l[i][j]:
c = 1
if i-1 >= 0 and j-1 >= 0 and l[i-1][j-1] >= l[i][j]:
c = 1
if i-1 >= 0 and j+1 < m and l[i-1][j+1] >= l[i][j]:
c = 1
if i+1 < n and j-1 >= 0 and l[i+1][j-1] >= l[i][j]:
c = 1
if i+1 < n and j+1 < m and l[i+1][j+1] >= l[i][j]:
c = 1
if c == 0:
count += 1
print(count)
Explanation:
1. Accept the dimension of the matrix and a matrix as input and assign count = 0.
2. Using two for loops, iterate through each and every element. Set a flag as 0 (here the flag is c).
3. Check if it is possible to move in all directions. If it is possible to move in a certain direction, check whether the element is greater than or equal to the current element.
4. If it greater, then set the flag (ie. c) as 1.
5. After checking in all possible directions, if the flag is not set, increment the count variable. This indicates that no surrounding element is greater than the current element.
6. Print the count.
Comments
Post a Comment