tinycodinglm / misc /misc-0354.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 16)
d517dc9 verified
|
Raw History Blame
3.16 kB
opencode trace
System You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code. When solving problems, explain your reasoning step by step before providing...

You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code. When solving problems, explain your reasoning step by step before providing the final solution.

User

Definition

An element is leader if it is greater than The Sum all the elements to its right side.


Task

Given an array/list [] of integers , Find all the LEADERS in the array.


Notes

  • Array/list size is at least 3 .

  • Array/list's numbers Will be mixture of positives , negatives and zeros

  • Repetition of numbers in the array/list could occur.

  • Returned Array/list should store the leading numbers in the same order in the original array/list .


Input >> Output Examples

markdown
arrayLeaders ({1, 2, 3, 4, 0}) ==> return {4}

Explanation:

  • 4 is greater than the sum all the elements to its right side

  • Note : The last element 0 is equal to right sum of its elements (abstract zero).


markdown
arrayLeaders ({16, 17, 4, 3, 5, 2}) ==> return {17, 5, 2}

Explanation:

  • 17 is greater than the sum all the elements to its right side

  • 5 is greater than the sum all the elements to its right side

  • Note : The last element 2 is greater than the sum of its right elements (abstract zero).


markdown
arrayLeaders ({5, 2, -1}) ==> return {5, 2}

Explanation:

  • 5 is greater than the sum all the elements to its right side

  • 2 is greater than the sum all the elements to its right side

  • Note : The last element -1 is less than the sum of its right elements (abstract zero).


markdown
arrayLeaders ({0, -1, -29, 3, 2}) ==> return {0, -1, 3, 2}

Explanation:

  • 0 is greater than the sum all the elements to its right side

  • -1 is greater than the sum all the elements to its right side

  • 3 is greater than the sum all the elements to its right side

  • Note : The last element 2 is greater than the sum of its right elements (abstract zero).

Starter code:
def array_leaders(numbers):

Assistant

def array_leaders(numbers):
n = len(numbers)
if n == 0:
return []

markdown
suffix_sums = [0] * n
suffix_sums[-1] = 0  # The last element has no elements to the right

# Compute suffix sums from the second last element down to 0
for i in range(n - 2, -1, -1):
    suffix_sums[i] = numbers[i + 1] + suffix_sums[i + 1]

# Collect all elements that are greater than their suffix sum
result = []
for i in range(n):
    if numbers[i] > suffix_sums[i]:
        result.append(numbers[i])

return result