We just brute force our way into the matrix, do bound checks for our values and just calculate the required hour glass sum, and finally update our max sum with every iteration.

Code

Python

class Solution:
    def rearrangeArray(self, nums: List[int]) -> List[int]:
        pos, neg = 0, 1
 
        res = [0 for _ in range(len(nums))]
 
        for n in nums:
            if n > 0:
                res[pos] = n
                pos += 2
            if n < 0:
                res[neg] = n
                neg += 2
        
        return res

Big O Analysis

  • Runtime

    The runtime complexity here is since we visit all elements atleast once.

  • Memory

    The memory usage is since we are creating a new result array.

— A

GitHub | Twitter