Find the weight of five apples

Problem

John bought 5 apples. You are given the weights of every group of four apples, and must then find the weights of the apples themselves.

For example, if all apples without the first one weigh 798 g, without the second - 794 g, without the third - 813 g, without the fourth - 806 g, and without the fifth - 789 g, the weights are 202, 206, 187, 194, and 211.

Rules

1. The solution of the problem by enumeration is allowed
2. Consider the number of points as follows: the number of bytes in your code. The lower the number of points, the better.

Have fun!

• Welcome to Code Golf! You'll need to clarify a few parts of this, like whether it will always be five apples and what the allowed ranges of inputs will be. I also don't think fastest code is worth including here, since IIRC there's going to be a constant-time solution to this. Apr 6 at 16:13
• Also, the solution will likely run in microseconds at most. In order to make this possible to accurately time, it would need to be run millions of times in a row. And relying on the user's CPU leads to unfairness, since people with faster computers will score better. Apr 6 at 16:15
• I don't think this works as [fastest-code]. You can get the total weight of the apples by dividing the sum of your numbers by 4, then subtract each of your numbers to get the weight of that apple (This is the only solution). 5 additions, 5 subtractions and a single division is not something you really measure in seconds.
– ovs
Apr 6 at 16:23
• @radvylf-programs and ovs, fixed this. Thanks! Apr 6 at 16:39
• I recommend removing rule 1 and sticking with the site defaults for IO, which avoids boilerplate code and concentrates the task down to its core. (Note that none of the answers so far implement this rule, probably because people are so used to using the site defaults.) Apr 6 at 21:08

MathGolf, 3 (or 5) bytes

Σ¼,


No idea why MathGolf has a single-byte a//4 builtin, but it's pretty useful here. ;)

Try it online.

With strict input of space-delimited values it would be 5 bytes instead:

ê_Σ¼,


Try it online.

Explanation:

     # Optional for stricter space-delimited input:
ê    # Push the inputs as integer-array
_   # Duplicate it

Σ    # Sum the values together
¼   # Integer-divide it by 4
,  # Subtract each value in the list from this sum//4
# (after which the entire stack is output implicitly)

• Surely if the sum//4 is lower than an individual value, that means that one of the apples actually does have a negative mass (and so no need to take absolute value...)? Apr 7 at 9:57
• @DominicvanEssen I was indeed doubting a bit whether it was necessary, but you're indeed completely right! Apr 7 at 9:58
• Nice to see a MathGolf answer! To answer why there is a floor division by 4 operator, I had just implemented the ½ operator, and the next character in line was ¼. I knew it would be useful one of these days!
– maxb
Apr 8 at 13:49

Vyxal, 976 5 bytes

∑4ḭ$-  Try it Online! Explanation ∑4ḭ$-
# (implicit input)
∑         # Sum
4ḭ       # Divide the sum by four
$- # Swap and subtract  -2 bytes thanks to a stone arachnid -1 byte thanks to ovs • ∑4ḭ$- for 5 bytes
– ovs
Apr 8 at 8:45

BQN, 8 6 bytes

Edit: -2 bytes thanks to ovs

-+´÷⟜4


Try it at BQN online REPL

    ÷⟜4     # divide each of the input values by 4
+´         # and then fold the 'plus' function across them
-           # using the negative of the input as a list of starting values
# (so effectively we start the fold using each negative input value
# in parallel as a starting value)

• -+´÷⟜4 works for 6 bytes
– ovs
Apr 7 at 10:49
• @ovs - Thanks! Can you explain how it works? In particular, how does +´ = fold plus work as a dyadic operator? Apr 7 at 10:58
• -+´÷⟜4 is just an ordinary fork. ´ when used dyadically does "fold with starting value". Apr 7 at 11:08
• @Razetime - But here both the left (- = negative of input) and right (÷⟜4 = input divided by 4) arguments are lists. How does that work to use them as a starting value? Apr 7 at 11:11
• arrays are fine to use as a starting value. adding an array and a number is okay in BQN. It distinguishes between arrays and atomic values (numbers, chars, so on) , so it doesn't fuss when given that as a starting value. Apr 7 at 12:25

Python, 32 bytes

lambda a:[sum(a)/4-x for x in a]

Attempt This Online!

map=<<(-).(/4).sum

Attempt This Online!

Japt v2.0a0 -m, 5 bytes

aWx÷4


Try it

Excel, 15 bytes

=SUM(A1#)/4-A1#


Input is in cell A1 as an array. For instance, ={798;794;813;806;789}

Excel, 19 bytes

=SUM(A1:A5)/4-A1:A5


Input is in the cells A1:A5. Doesn't rely on array input. Output is wherever the formula is. It's not a very interesting solution.

• If you take input as an array, this simplifies to Sum(A1#)/4-A1# May 18 at 0:20

TI-Basic, 7 bytes

sum(Ans)/4-Ans


Takes input in Ans. Output is stored in Ans and displayed.

Desmos, 16 bytes

f(l)=l.total/4-l


Try It On Desmos!

Try It On Desmos! - Prettified

LOLCODE, 216 bytes

HOW IZ I f YR a
I HAS A s ITZ 0
IM IN YR l UPPIN YR i TIL BOTH SAEM i 5
s R SUM OF s a'Z SRS i
IM OUTTA YR l
IM IN YR l UPPIN YR i TIL BOTH SAEM i 5
VISIBLE DIFF OF QUOSHUNT OF s 4 a'Z SRS i
IM OUTTA YR l
IF U SAY SO


Try it online!

λ-2d, 456 squares

the program can be imported in the playground with the following JSON file

{"41,13":"entry","41,18":"entry","41,23":"entry","41,28":"entry","41,32":"entry","42,13":"end_s","42,18":"end_s","42,23":"end_s","42,28":"end_s","42,32":"end_s","43,13":"end_e","43,18":"end_e","43,23":"end_e","43,28":"end_e","43,32":"end_e","39,15":"wire_nw","39,20":"wire_nw","39,25":"wire_nw","39,30":"wire_nw","39,34":"wire_nw","38,15":"wire_ne","38,20":"wire_ne","38,25":"wire_ne","38,30":"wire_ne","38,34":"wire_ne","44,13":"frame_tl","44,18":"frame_tl","44,23":"frame_tl","44,28":"frame_tl","44,32":"frame_tl","45,13":"wire_we","45,18":"wire_we","45,23":"wire_we","45,28":"wire_we","45,32":"wire_we","46,13":"wire_we","46,18":"wire_we","46,23":"wire_we","46,28":"wire_we","46,32":"wire_we","47,13":"wire_we","47,18":"wire_we","47,23":"wire_we","47,28":"wire_we","47,32":"wire_we","47,15":"wire_we","47,20":"wire_we","47,25":"wire_we","47,30":"wire_we","47,34":"wire_we","46,15":"wire_we","46,20":"wire_we","46,25":"wire_we","46,30":"wire_we","46,34":"wire_we","45,15":"wire_we","45,20":"wire_we","45,25":"wire_we","45,30":"wire_we","45,34":"wire_we","48,13":"wire_sw","48,18":"wire_sw","48,23":"wire_sw","48,28":"wire_sw","48,32":"wire_sw","48,15":"wire_nw","48,20":"wire_nw","48,25":"wire_nw","48,30":"wire_nw","48,34":"wire_nw","44,15":"wire_ne","44,20":"wire_ne","44,25":"wire_ne","44,30":"wire_ne","44,34":"wire_ne","44,14":"wire_ns","44,19":"wire_ns","44,24":"wire_ns","44,29":"wire_ns","44,33":"wire_ns","48,14":"wire_ns","48,19":"wire_ns","48,24":"wire_ns","48,29":"wire_ns","48,33":"wire_ns","42,14":"wire_nw","42,19":"wire_nw","42,24":"wire_nw","42,29":"wire_nw","42,33":"wire_nw","41,14":"wire_we","41,19":"wire_we","41,24":"wire_we","41,29":"wire_we","41,33":"wire_we","40,14":"wire_we","40,19":"wire_we","40,24":"wire_we","40,29":"wire_we","40,33":"wire_we","38,14":"wire_sw","38,19":"wire_sw","38,24":"wire_sw","38,29":"wire_sw","38,33":"wire_sw","39,14":"func_call","39,19":"func_call","39,24":"func_call","39,29":"func_call","39,33":"func_call","39,13":"joint_nsw","39,18":"joint_nsw","39,23":"joint_nsw","39,28":"joint_nsw","38,13":"wire_we","38,18":"wire_we","38,23":"wire_we","38,28":"wire_we","37,13":"wire_se","37,18":"wire_se","37,23":"wire_se","37,28":"wire_se","37,14":"wire_nswe","37,19":"wire_nswe","37,24":"wire_nswe","37,29":"wire_nswe","37,17":"wire_nw","37,22":"wire_nw","37,27":"wire_nw","37,32":"wire_nw","36,17":"wire_se","36,22":"wire_se","36,27":"wire_se","36,32":"wire_se","37,15":"wire_ns","37,20":"wire_ns","37,25":"wire_ns","37,30":"wire_ns","36,15":"wire_nw","36,20":"wire_nw","36,25":"wire_nw","36,30":"wire_nw","36,34":"wire_nw","35,15":"wire_ne","35,20":"wire_ne","35,25":"wire_ne","35,30":"wire_ne","35,34":"wire_ne","35,14":"wire_sw","35,19":"wire_sw","35,24":"wire_sw","35,29":"wire_sw","35,33":"wire_sw","36,14":"func_call","36,19":"func_call","36,24":"func_call","36,29":"func_call","36,33":"func_call","36,13":"joint_nsw","36,18":"joint_nsw","36,23":"joint_nsw","36,28":"joint_nsw","35,13":"wire_we","35,18":"wire_we","35,23":"wire_we","35,28":"wire_we","34,13":"wire_se","34,18":"wire_se","34,23":"wire_se","34,28":"wire_se","34,14":"wire_nswe","34,19":"wire_nswe","34,24":"wire_nswe","34,29":"wire_nswe","34,17":"wire_nw","34,22":"wire_nw","34,27":"wire_nw","34,32":"wire_nw","33,17":"wire_se","33,22":"wire_se","33,27":"wire_se","33,32":"wire_se","34,15":"wire_ns","34,20":"wire_ns","34,25":"wire_ns","34,30":"wire_ns","33,15":"wire_nw","33,20":"wire_nw","33,25":"wire_nw","33,30":"wire_nw","33,34":"wire_nw","32,15":"wire_ne","32,20":"wire_ne","32,25":"wire_ne","32,30":"wire_ne","32,34":"wire_ne","32,14":"wire_sw","32,19":"wire_sw","32,24":"wire_sw","32,29":"wire_sw","32,33":"wire_sw","33,14":"func_call","33,19":"func_call","33,24":"func_call","33,29":"func_call","33,33":"func_call","33,13":"joint_nsw","33,18":"joint_nsw","33,23":"joint_nsw","33,28":"joint_nsw","32,13":"wire_we","32,18":"wire_we","32,23":"wire_we","32,28":"wire_we","31,13":"wire_se","31,18":"wire_se","31,23":"wire_se","31,28":"wire_se","31,14":"wire_nswe","31,19":"wire_nswe","31,24":"wire_nswe","31,29":"wire_nswe","31,17":"wire_nw","31,22":"wire_nw","31,27":"wire_nw","31,32":"wire_nw","30,17":"wire_se","30,22":"wire_se","30,27":"wire_se","30,32":"wire_se","31,15":"wire_ns","31,20":"wire_ns","31,25":"wire_ns","31,30":"wire_ns","30,15":"wire_nw","30,20":"wire_nw","30,25":"wire_nw","30,30":"wire_nw","30,34":"wire_nw","29,15":"wire_ne","29,20":"wire_ne","29,25":"wire_ne","29,30":"wire_ne","29,34":"wire_ne","29,14":"wire_sw","29,19":"wire_sw","29,24":"wire_sw","29,29":"wire_sw","29,33":"wire_sw","30,14":"func_call","30,19":"func_call","30,24":"func_call","30,29":"func_call","30,33":"func_call","30,13":"joint_nsw","30,18":"joint_nsw","30,23":"joint_nsw","30,28":"joint_nsw","29,13":"wire_we","29,18":"wire_we","29,23":"wire_we","29,28":"wire_we","28,13":"wire_se","28,18":"wire_se","28,23":"wire_se","28,28":"wire_se","28,14":"wire_nswe","28,19":"wire_nswe","28,24":"wire_nswe","28,29":"wire_nswe","28,17":"wire_nw","28,22":"wire_nw","28,27":"wire_nw","28,32":"wire_nw","27,17":"wire_se","27,22":"wire_se","27,27":"wire_se","27,32":"wire_se","28,15":"wire_ns","28,20":"wire_ns","28,25":"wire_ns","28,30":"wire_ns","27,14":"func_call","27,19":"func_call","27,24":"func_call","27,29":"func_call","27,33":"func_call","27,13":"joint_nsw","27,18":"joint_nsw","27,23":"joint_nsw","27,28":"joint_nsw","26,13":"wire_se","26,18":"wire_se","26,23":"wire_se","26,28":"wire_se","26,14":"wire_ns","26,19":"wire_ns","26,24":"wire_ns","26,29":"wire_ns","26,15":"wire_ns","26,20":"wire_ns","26,25":"wire_ns","26,30":"wire_ns","26,16":"wire_ne","26,21":"wire_ne","26,26":"wire_ne","26,31":"wire_ne","27,16":"wire_we","27,21":"wire_we","27,26":"wire_we","27,31":"wire_we","29,16":"wire_we","29,21":"wire_we","29,26":"wire_we","29,31":"wire_we","30,16":"wire_we","30,21":"wire_we","30,26":"wire_we","30,31":"wire_we","32,16":"wire_we","32,21":"wire_we","32,26":"wire_we","32,31":"wire_we","33,16":"wire_we","33,21":"wire_we","33,26":"wire_we","33,31":"wire_we","35,16":"wire_we","35,21":"wire_we","35,26":"wire_we","35,31":"wire_we","36,16":"wire_we","36,21":"wire_we","36,26":"wire_we","36,31":"wire_we","39,16":"wire_sw","39,21":"wire_sw","39,26":"wire_sw","39,31":"wire_sw","39,17":"wire_ns","39,22":"wire_ns","39,27":"wire_ns","39,32":"wire_ns","38,16":"wire_we","38,21":"wire_we","38,26":"wire_we","38,31":"wire_we","28,16":"wire_nswe","28,21":"wire_nswe","28,26":"wire_nswe","28,31":"wire_nswe","31,16":"wire_nswe","31,21":"wire_nswe","31,26":"wire_nswe","31,31":"wire_nswe","34,16":"wire_nswe","34,21":"wire_nswe","34,26":"wire_nswe","34,31":"wire_nswe","37,16":"wire_nswe","37,21":"wire_nswe","37,26":"wire_nswe","37,31":"wire_nswe","28,33":"wire_we","31,33":"wire_we","34,33":"wire_we","37,33":"wire_we","39,12":"num_7","40,12":"num_8","41,12":"num_9","36,11":"num_8","37,11":"num_0","38,11":"num_6","33,12":"num_8","34,12":"num_1","35,12":"num_3","30,11":"num_7","31,11":"num_9","32,11":"num_4","28,12":"num_9","29,12":"num_8","27,12":"num_7","30,12":"wire_ns","36,12":"wire_ns","27,15":[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0],"27,20":[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0],"27,25":[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0],"27,30":[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0],"27,34":[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0],"53,12":"func_def","55,12":"func_def","57,12":"func_def","59,12":"func_def","61,12":"func_def","54,12":"end_s","58,12":"end_s","56,12":"end_s","60,12":"end_s","62,12":"end_s","63,12":"end_e","54,11":"label","55,11":[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0],"53,11":"wire_se","54,13":"joint_nse","55,16":"op_plus","57,16":"op_plus","59,16":"op_plus","61,16":"op_plus","55,15":"func_call","57,15":"func_call","59,15":"func_call","61,15":"func_call","56,13":"wire_nswe","58,13":"wire_nswe","61,13":"wire_we","60,13":"wire_nswe","62,13":"wire_nswe","59,13":"wire_we","57,13":"wire_we","55,13":"wire_we","55,14":"wire_sw","54,14":"wire_ne","56,15":"wire_nw","58,15":"wire_nw","60,15":"wire_nw","57,14":"wire_sw","59,14":"wire_sw","60,14":"func_call","61,14":"wire_sw","56,14":"func_call","58,14":"func_call","62,14":"func_call","62,15":"wire_nw","63,14":"wire_sw","63,15":"wire_ns","63,16":"func_call","63,17":"op_div","64,16":"wire_nw","64,15":"func_call","64,14":"num_4","65,15":"wire_sw","65,16":"func_call","65,17":"op_minus","66,16":"wire_nw","66,14":"func_call","67,14":"wire_nw","66,13":"wire_sw","67,12":"wire_sw","66,15":"wire_ns","67,13":"wire_ns","63,13":"wire_we","64,13":"wire_we","65,13":"wire_we","64,12":"wire_we","65,12":"wire_we","66,12":"wire_we"}


the function in itself is the right structure, the left one being 5 calls to the function with the 5 arguments in differents orders

js equivalent

a=798
b=794
c=813
d=806
e=789
f=v=>w>=>x=>y=>z=>((v+w+x+y+z)/4)-v

f(a)(b)(c)(d)(e)
f(b)(c)(d)(e)(a)
f(c)(d)(e)(a)(b)
f(d)(e)(a)(b)(c)
f(e)(a)(b)(c)(d)


Language explanation can be found in the playground by loading the cheatsheet example, or in the release blog post

Pip, 9 7 bytes

$+a/4-a Attempt This Online! -2 bytes thanks to DLosc • Since arithmetic operators apply to lists itemwise, $+a/4-a works for 7 bytes. Attempt This Online! Apr 14 at 16:25

Jelly, 4? 8 bytes

With a strict interpretation of Rule 1 (can't take as a program argument due to the "single line" part) - a full program that reads from STDIN and writes to STDOUT:

ɠḲVµS÷4_


Try it online!

...or with site default IO instead - a monadic Link that accepts a list of five numbers and yields a list of five numbers:

S÷4_


Try it online!

How?

ɠḲVµS÷4_ - Main Link: no arguments
ɠ        - read a line from STDIN
Ḳ       - split that at spaces
V      - evaluate that as Jelly code -> list of the five four-apple-weights, W
µ     - start a new monadic chain - f(W)
S    - sum W
4  - four
÷   - divide -> sum(W)/4
_ - subtract W (vectorises) -> [sum(W)/4-w1, sum(W)/4-w2, sum(W)/4-w3, sum(W)/4-w4, sum(W)/4-w5]
- implicit print


Factor + math.unicode, 23 bytes

[ dup Σ 4 / swap n-v ]


Try it online!

Explanation

       ! { 798 794 803 816 789 }
dup    ! { 798 794 803 816 789 } { 798 794 803 816 789 }
Σ      ! { 798 794 803 816 789 } 4000
4      ! { 798 794 803 816 789 } 4000 4
/      ! { 798 794 803 816 789 } 1000
swap   ! 1000 { 798 794 803 816 789 }
n-v    ! { 202 206 197 184 211 }


Ruby, 22 bytes

->a{a.map{a.sum/4-_1}}

Attempt This Online!

Julia 1.0, 14 bytes

!l=sum(l/4).-l


Try it online!

05AB1E, 9 4 bytes

O4÷α


Try it online!

-5 bytes thanks to Kevin Cruijssen

• Input is done implicitly in 05AB1E, so you can remove the leading I. :) 05AB1E also vectorizes in a lot of cases, so you don't need the map here if you use a Duplicate of the list instead. To summarize, this could be 6 bytes: #DO4÷α, or with default I/O as lists it could be 4 bytes: O4÷α. Apr 7 at 9:16

R, 14 bytes

\(x)sum(x)/4-x


Try it online!

Uses the new lambda, \, introduced in R 4.1.

J, 7 bytes

-~4%~+/

Attempt This Online!

jq, 20 9 bytes

add/4-.[]

Attempt This Online!

-11 bytes thanks to @ovs

• [add/4-.[]] works for 11. And I'd say you can omit the outer pair of brackets for 9
– ovs
Apr 8 at 8:41

Burlesque, 9 bytes

J++4./j?-


Try it online!

J   # Duplicate
++  # Sum
4./ # Divide by 4
j   # Swap
?-  # Subtract from each


Halfwit, 5 bytes

kJ>+<k+N+N


Try it online.

Inputs as a list of BigInts.

Explanation:

kJ          # Sum the (implicit) input-list
>+<       # Push compressed BigInt 4n
k+     # Integer-divide the sum by this 4
N+N  # Subtract the values in the (implicit) input-list from this value:
N    #  Negate the value
+   #  Add it to each value in the (implicit) input-list
N  #  Negate each value in the list
# (after which the result is output implicitly)

• Woah, nice! I’m glad I made this lang somewhat usable. You can use the compressed encoding if you want (S instead of kJ, / instead of k+). I’ll try to fix the bigint/number bug at some point Apr 30 at 9:16
• @emanresuA Yeah, I know about the S and / instead of kJ and k+, but I liked to write the full-out version so you know the amount of characters halved is the byte-count. :) Btw, is >+< the proper/shortest way to push 4n, or is there a better alternative? Apr 30 at 11:40

C (gcc), 61 58 bytes

n,i;f(int*a){for(i=10;i--;)i>4?n+=a[i%5]:(a[i]=n/4-a[i]);}


Try it online!

-3 bytes thanks to ceilingcat

x86 32-bit machine code, 32 bytes

00000000: 87ca 31c0 6a05 5903 448a fce2 fac1 e802  ..1.j.Y.D.......
00000010: 6a05 5950 2b44 8afc 8944 8afc 58e2 f4c3  j.YP+D...D..X...


Assembly

section .text
global func
func:					;void func(int *ecx);
; int *edx=ecx; int eax=0;
xchg ecx, edx
xor eax,eax

; for(ecx=5;ecx>0;ecx--)eax+=edx[ecx-1]
push 0x5
pop ecx

; eax = eax / 4
shr eax, 2

; for(ecx=5;ecx>0;ecx--)edx[ecx-1]=eax-edx[ecx-1];
push 0x5
pop ecx
sub:
push eax
sub eax, [edx + 4*ecx-4]
mov [edx + 4*ecx-4], eax
pop eax
loop sub

; return
ret


Takes a pointer to an array of 5 integers in ECX (fastcall convention), and modifies the array in place with the results.

Try it online!

Charcoal, 7 bytes

Ｉ⁻÷Σθ⁴θ


Try it online! Link is to verbose version of code. Explanation:

    θ   Input list
Σ    Take the sum
÷     Integer divided by
⁴  Literal integer 4
⁻      Vectorised subtract
θ Input list
Ｉ       Cast to string
Implicitly print


9 bytes for a version that works with two or more apples, not just five:

Ｉ⁻÷Σθ⊖Ｌθθ


Try it online! Link is to verbose version of code.

JavaScript (Node.js), 34 32 bytes

a=>a.map(e=>eval(a.join+)/4-e)

Attempt This Online!

-2 bytes thanks to Steffan

Red, 28 bytes

func[v][v -((sum v)/ 4)* -1]


Try it online!

C (clang), 60 bytes

b,c;f(*a){for(b=c=0;b<5;c+=a[b++]);for(;b--;a[b]=c/4-a[b]);}


Try it online! Takes an array of 5 integers and modifies the array in-place.

PARI/GP, 23 bytes

a->[vecsum(a)/4-t|t<-a]

Attempt This Online!

Wolfram Language (Mathematica), 9 bytes

Tr@#/4-#&


Try it online!