Maximum Direction in a Matrix

The Great Researcher Mr. Bari will give you some chocolates from Singapore if you can solve the foll…

Click here to read the complete problem statement.


If you need help solving this problem, mention your approach and ask specific questions. Please avoid sharing your code and asking the Community to figure out “what’s wrong”.

Please format your code properly before posting.
You can use this as a reference.
And it will be better if you explain your code also,
I can’t understand it.
So, edit your Post.

re: I don’t see you using the array module anywhere in your code. So, why are you importing this actually?

n=int(input())
T=[]
m=[]
f=l=j=0
for i in range(n):
    T.insert(i,list(map(int, input().split())))

for i in range(n):
    m.append(sum(T[i]))
for i in range(n):
    f+=T[i][i]
    m.append(f)
for i in range(n):
    l+=T[i][-i]
    m.append(l)
for k in range(n):
    for i in range(n):
        j+=T[i][k]
    m.append(j)
    j=0
print(max(m))

Firstly, it makes an n*n matrix while taking n number of rows as inputs. Then calculates the sums of each row and puts it in list m. Then it works with the diagonals where their sums are l and f. Both are kept in m then. At last it works with each column and saves their sum in m. At last the output is the maximum value of m.
My bad that I imported array first, it wasn’t necessary tho.

Well, this is actually very silly & common that I also face often. Fix this and the code should work.

for i in range(n):
    f += T[i][i]
    m.append(f) # You should put this line out of the loop,

for i in range(n):
    l += T[i][-i]
    m.append(l) # and this also.

And also, please delete your previous unformatted code. I meant this post. It would have been better if you had edited your previous post rather than creating a new one.

Anyways, if you still has problems then you are free to @-mention me. I will try to response quickly.

@touhidur

for i in range(n):
    f+=T[i][i]
m.append(f)
for i in range(n):
    l+=T[i][-i]
m.append(l)

Changed my code the way you mentioned. Still it shows ‘wrong answer’ for the 4th test.

for i in range(n):
    l+=T[i][-i-1] # indexing problem 
m.append(l)
1 Like