Given a list of numbers, return a new list consists of the number which is larger than the sum of the remaining numbers in a list or larger than absolute zero. For example, this list [2, 5, 2, 1] will convert to [5, 2, 1] because 5 is greater than 2+1, 2 is greater than 1 and 1 is greater than absolute zero. Another example. [2, 3, -3] will convert to [2, 3] because 2 is greater than 3+(-3) and 3 is greater than -3 but -3 is lesser than 0 which means -3 will not get included into the new list.

def array_leaders(numbers):
    arr = [] # the new list to store the leading numbers
        leader = numbers.pop(0)
        # return the array if there is nothing to sum up
        if(len(numbers) == 0 and leader > 0):
            return arr
        elif(len(numbers) == 0):
            return arr

        sum = 0
        for elem in numbers:
            sum+=elem #add up all other numbers
        if leader > sum:
            arr.append(leader) # if the leading number is larger than the total then append that number to the number leader list

If you have a better solution then please provide your answer in the comment box below.

Click to rate this post!
[Total: 0 Average: 0]
Please follow and like us:


  1. def array_leaders(numbers):
    if len(numbers) == 0:
    return []
    if numbers[0] > sum(numbers[1:]):
    return [numbers[0]] + array_leaders(numbers[1:])
    return array_leaders(numbers[1:])

  2. Here’s a way with cumulative summation in one step, then using that as a filter in another step. Not sure how it measures up in efficiency, and readability is usually subjective.

    from itertools import accumulate
    from operator import add

    def more_than_rest(numbers):
    backward_sums = accumulate(reversed(numbers[1:] + [0]), add)
    sums = reversed(list(backward_sums))
    return [x for x, s in zip(numbers, sums) if x > s]

Leave a Reply

Your email address will not be published. Required fields are marked *