176 Views

Print the largest (maximum) hourglass sum found in array


Harsh Raj 13th May, 2021
Answer (1)
Palak kumari 13th May, 2021

Hi there,

def array2D(arr):


# want to find the maximum hourglass sum

# minimum hourglass sum = -9 * 7 = -63

maxSum = -63

for i in range(4):

for j in range(4):

# sum of top 3 elements

top = sum(arr[i][j:j+3])

# sum of the mid element

mid = arr[i+1][j+1]

# sum of bottom 3 elements

bottom = sum(arr[i+2][j:j+3])

hourglass = top + mid + bottom

if hourglass > maxSum:

maxSum = hourglass

return maxSum

Related Questions

MAHE Manipal M.Tech 2025
Apply
NAAC A++ Accredited | Accorded institution of Eminence by Govt. of India | NIRF Rank #4
University of Southampton Del...
Apply
MSc Finance and MSc International Management Admissions 2025 Now Open | Ranked Among the Top 100 Universities in the World by QS World University R...
Amity University, Noida Law A...
Apply
700+ Campus placements at top national and global law firms, corporates, and judiciaries
Chandigarh University Admissi...
Apply
Ranked #1 Among all Private Indian Universities in QS Asia Rankings 2025 | Scholarships worth 210 CR
Amrita Vishwa Vidyapeetham | ...
Apply
Recognized as Institute of Eminence by Govt. of India | NAAC ‘A++’ Grade | Upto 75% Scholarships
UPES B.Tech Admissions 2025
Apply
Ranked #42 among Engineering colleges in India by NIRF | Highest CTC 50 LPA , 100% Placements
View All Application Forms

Download the Careers360 App on your Android phone

Regular exam updates, QnA, Predictors, College Applications & E-books now on your Mobile

150M+ Students
30,000+ Colleges
500+ Exams
1500+ E-books