Google | Phone Screen | L4

Anonymous User
1338
Mar 14, 2025
Mar 14, 2025

An array with values is given. After removing an element, find whether the array can be divided into two parts, where the sum has to be equal and elements has to be continuous.

Eg: [3, 1, 1, 1, 7] -> removing 7 -> True. (3) & (1, 1, 1)
[1, 3, 1, 1, 7] -> removing 7 -> False, since the array can't be divided into two parts.

Follow-up: if the array is circular.
Eg: [1, 3, 1, 1, 7] -> removing 7 -> True, (1, 1, 1) & (3)

I provided O(N * N) time and O(N) space for both, coded original, explained follow-up.

My code for main question:

def divide(arr):
    def canDivide(arr1):
        total = sum(arr1)
        half = total // 2
        curSum = 0

        for n in arr1:
            curSum += n

            if curSum == half:
                return True
            elif curSum > half:
                break

        return False

    for i in range(len(arr)):
        arr1 = arr[:]
        arr1.remove(arr1[i])
        if canDivide(arr1):
            return True

    return False

#google

Comments (8)