22
3
Upside-Down Pyramid Addition is the process of taking a list of numbers and consecutively adding them together until you reach one number.
When given the numbers 2, 1, 1 the following process occurs:
2 1 1
3 2
5
This ends in the number 5.
YOUR TASK
Given the right side of an Upside-Down Pyramid (Ascending), write a program or function that will return the original list.
New Extra Challenge: Try doing this in less than O(n^2)
EXAMPLE
f([5, 2, 1]) => [2, 1, 1]
f([84,42,21,10,2]) => [4,7,3,8,2]
NOTE: The Upside-Down Pyramid will never be empty and will always consist of positive integers ONLY.
6
Welcome to PP&CG! This challenge is decent, though it could be improved. I'd recommend posting your challenges in the Sandbox first to improve the post before it's put on main.
– Tau – 7 years agoDo the input and output have to be in the order given (top to bottom and left to right respectively), or can either or both be reversed? – Nick Kennedy – 7 years ago
The input will go from bottom to top. The output must be from left to right – Whimpers – 7 years ago
13Free insight that I can't find a language it's shorter in: f([a,b,c,d,e])=[1−46−41 01−33−1 001−21 0001−1 00001]⋅[a\b\c\d\e] – Lynn – 7 years ago
4
Just FYI, this is the same as the CodeWars kata.
– ggorlen – 7 years ago6@ggorlen i know. I’m the one who made the kata :) – Whimpers – 7 years ago
8
Try doing this in less than O(n)surely it's impossible to allocate a n-sized array or change O(n) items in it faster than O(n) complexity? – my pronoun is monicareinstate – 7 years ago3Suggested test case:
1 => 1– Giuseppe – 7 years ago@someone fixed, thanks for catching that, I meant quadratic time, not linear. – Whimpers – 7 years ago