Instagram
youtube
Facebook
Twitter

2D Array-DS| Array

Task(easy)

Given a 6*6 2D Array,arr :

1 1 1 0 0 0

0 1 0 0 0 0

1 1 1 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

An hourglass in A is a subset of values with indices falling in this pattern in arr's graphical representation:

a b c

  d

e f g

There are 16 hourglasses in arr. An hourglass sum is the sum of an hourglass' values. Calculate the hourglass sum for every hourglass in arr, then print the maximum hourglass sum. The array will always be 6*6.

Example

-9 -9 -9 1 1 1 

 0 -9 0 4 3 2

-9 -9 -9 1 2 3

 0  0  8 6 6 0

 0  0  0-2 0 0

 0  0  1 2 4 0

The 16 hourglass sums are:

-63, -34, -9, 12, 

-10,   0, 28, 23, 

-27, -11, -2, 10, 

  9,  17, 25, 18

The highest hourglass sum is 28 from the hourglass beginning at row 1, column 2:

0 4 3

  1

8 6 6

Note: If you have already solved the Java domain's Java 2D Array challenge, you may wish to skip this challenge.

Function Description

Complete the function hourglassSum in the editor below.

hourglassSum has the following parameter(s):

  • int arr[6][6]: an array of integers

Returns

  • int: the maximum hourglass sum

Input Format

Each of the 6 lines of inputs arr[i] contains 6 space-separated integers arr[i][j] .

Constraints

-9≤arr[i][j]<9
0≤I,j≤5

Output Format

Print the largest (maximum) hourglass sum found in arr.

Sample Input

1 1 1 0 0 0

0 1 0 0 0 0

1 1 1 0 0 0

0 0 2 4 4 0

0 0 0 2 0 0

0 0 1 2 4 0

Sample Output

19

Solution 1

def hourglassSum(arr):

    new_array = []

    for i in range(len(arr)):

        for j in range(len(arr[i])):

            new_array.append(arr[i][j:j+3])

    second_array = []

    for i in range(len(new_array)-13):

        if len(new_array[i]) == 3:

            temp = []

            temp.extend(new_array[i])

            temp.extend(new_array[i+12])

            temp.extend(new_array[i+6][1:2])

            second_array.append(sum(temp))

    return max(second_array)

if __name__ == '__main__':

    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    ARRAY = []

    for _ in range(6):

        ARRAY.append(list(map(int, input().rstrip().split())))

    RESULT = hourglassSum(ARRAY)

    fptr.write(str(RESULT) + '\n')

    fptr.close()

Solution 2

def hourglassSum(arr):

    max_sum = float('-inf')  # Initialize to a very small number

    for i in range(4):  # Iterate over possible top-left corners

        for j in range(4):

            # Calculate the sum of the current hourglass

            top = arr[i][j] + arr[i][j + 1] + arr[i][j + 2]

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

            bottom = arr[i + 2][j] + arr[i + 2][j + 1] + arr[i + 2][j + 2]

            hourglass = top + middle + bottom

            # Update the maximum hourglass sum found

            if hourglass > max_sum:

                max_sum = hourglass

    return max_sum

EXPLAINATION STEPS

1.  Initialization: max_sum is initialized to negative infinity to ensure any hourglass sum will be larger.

2. Iterate Over Possible Hourglass Positions: Loop through all possible starting points for hourglasses. The top-left corner of an hourglass can range from (0, 0) to (3, 3) in a 6x6 array.

3. Calculate Hourglass Sum: For each starting point (i, j), compute the sum of the hourglass:

  • Top: Sum of the first row of the hourglass.
  • Middle: The middle element of the hourglass.
  • Bottom: Sum of the last row of the hourglass.

4. Update Maximum: Compare the current hourglass sum with max_sum and update if the current hourglass sum is greater.

5. Return Result: After checking all possible hourglasses, return the maximum sum.