28
\$\begingroup\$

Task

Given an array of positive integers, replace each element with the parity of the sum of the other elements. The array is guaranteed to have at least 2 elements.

Definition

  • Parity: whether a number is odd or even.

Example

For the array [1,2,3,1]:

  • Replace 1 with the parity of 2+3+1, i.e. even.
  • Replace 2 with the parity of 1+3+1, i.e. odd.
  • Replace 3 with the parity of 1+2+1, i.e. even.
  • Replace 1 with the parity of 1+2+3, i.e. even.

Output: [even, odd, even, even]

Input

An array of positive integer.

You may take it as a proper array, or as a linefeed-separated string of positive integers.

You may assume that the array and the values inside are within the handling capability of your language.

Output

An array of two consistent values, one representing odd, one representing even.

You may output it as a linefeed-separated string of the two values.

Testcases

Inputs:

[1, 2, 3, 1]
[1, 2, 3, 2, 1]
[2, 2]
[100, 1001]

Outputs:

[even, odd, even, even]
[even, odd, even, odd, even]
[even, even]
[odd, even]

Note: you may choose other consistent values other than odd and even.

Scoring

This is . Shortest answer in bytes wins.

Standard loophole applies.

asked May 2, 2017 at 18:27
\$\endgroup\$
1
  • \$\begingroup\$ Just a question - can you always recover the parity of the other elements? \$\endgroup\$ Commented May 24, 2021 at 19:58

41 Answers 41

1
2
17
\$\begingroup\$

Jelly, 3 bytes

+SḂ

Try it online!

How it works

+SḂ Main link. Argument: A (array)
 S Compute the sum of A.
+ Add the sum to each element of A.
 Using _ (subtraction) or ^ (bitwise XOR) would also work.
 Ḃ Bit; compute the parity of each resulting integer.
answered May 2, 2017 at 18:42
\$\endgroup\$
5
  • \$\begingroup\$ That's a clever approach. \$\endgroup\$ Commented May 2, 2017 at 18:42
  • 2
    \$\begingroup\$ @LeakyNun It's the same approach everyone else is using, only shorter :P \$\endgroup\$ Commented May 2, 2017 at 18:46
  • \$\begingroup\$ @ETHproductions Pretty much, yes. Thete are only so many ways to compute the parity... \$\endgroup\$ Commented May 2, 2017 at 18:48
  • \$\begingroup\$ @ETHproductions this uses addition instead of subtraction... \$\endgroup\$ Commented May 2, 2017 at 18:48
  • \$\begingroup\$ @LeakyNun True, my Japt answer does as well. In Jelly it'd just be _SḂ \$\endgroup\$ Commented May 2, 2017 at 18:49
8
\$\begingroup\$

JavaScript (ES6), (削除) 38 36 (削除ここまで) 32 bytes

a=>a.map(b=>eval(a.join`+`)-b&1)

Uses 0 for even and 1 for odd.

Test

f=
a=>a.map(b=>eval(a.join`+`)-b&1)
console.log(f([1, 2, 3, 1]))
console.log(f([1, 2, 3, 2, 1]))
console.log(f([100, 1001]))

answered May 2, 2017 at 18:32
\$\endgroup\$
3
  • \$\begingroup\$ 2 bytes off: c-b&1 instead of (c-b)%2 \$\endgroup\$ Commented May 2, 2017 at 18:34
  • \$\begingroup\$ Bah! You beat me to it! \$\endgroup\$ Commented May 2, 2017 at 18:44
  • 3
    \$\begingroup\$ I'll have to remember to use eval(a.join`+`) over a.reduce((x,y)=>x+y). That's clever \$\endgroup\$ Commented May 4, 2017 at 2:03
8
\$\begingroup\$

Haskell, 20 bytes

f x=odd.(sum x-)<$>x

Uses True for odd values and Falsefor even values.

Try it online!

Subtract each element from the sum of the list and test if it's odd.

f turned to pointfree has also 20 bytes: map=<<(odd.).(-).sum.

answered May 2, 2017 at 19:40
\$\endgroup\$
7
\$\begingroup\$

MATL, (削除) 5 (削除ここまで), 4 bytes

ts-o

Try it online!

One byte saved thanks to Dennis!

This gives '1' for odd and '0' for even. Explanation:

t % Duplicate the input
 s % Get the sum of the input
 - % Subtract it from the original input
 o % Get the parity of each element
answered May 2, 2017 at 19:17
\$\endgroup\$
0
6
\$\begingroup\$

Alice, (削除) 31 (削除ここまで) 28 bytes

/O.HQ\d$K@
\i#\ /d2-&+!w?+2%

Try it online!

The input format doesn't matter as long as the integers are separated. The output format is linefeed-separated.

The layout is probably still not optimal but I haven't found a way to shorten this further yet.

Explanation

/ Reflect to SE. Switch to Ordinal.
i Read all input as a string.
 Reflect off bottom boundary. Move to NE.
. Duplicate the input string.
 Reflect off top boundary. Move to SE.
\ Reflect to N. Switch to Cardinal.
H Implicitly convert the top copy of the input to the integers it
 contains and take the absolute value of the top-most one. (Taking
 the absolute value doesn't do anything, but we need the implicit
 conversion to integers.)
 The IP wraps back to the second line.
\ Reflect to SE. Switch to Ordinal.
 Immediately reflect off bottom boundary. Move to NE.
Q Reverse the stack (this converts the integers back to strings but
 that's irrelevant). After this, we end up with all the individual
 integers on the bottom of the stack in reverse order and the other
 copy of the input string with all integers on top.
 Reflect off top boundary. Move to SE.
/ Reflect to E. Switch to Cardinal.
d Push the stack depth, which is one more than the number of list
 elements (due to the other input string on the stack).
2- Subtract 2.
&+ Run + that many times, which implicitly converts the second string
 to the integers it contains and then adds up all the list elements.
! Store the sum on the tape.
w Push the current IP position to the return address stack. This
 lets us return here repeatedly to implement a loop.
 ?+ Retrieve the input sum from the tape and add it to the current
 element.
 2% Compute its parity. Other ways to do this are 1A and 0x. 2F would
 also work but it gives 0/2 instead of 0/1.
 The IP wraps the first column of the grid.
 \ Reflect to NE. Switch to Ordinal. The IP bounces diaginally up
 and down until it hits the next \.
 O Implicitly convert the current parity to a string and print it
 with a trailing linefeed.
 # Skip the next command (the H).
 \ Reflect to E. Switch to Cardinal.
 d Push the stack depth. This is zero when we're done.
 $ Skip the next command if the stack depth is indeed zero, which
 exits the loop.
K Jump to the return address on top of the return address stack without
 popping it (so that the next K will jump there again).
@ Terminate the program.
answered May 2, 2017 at 20:34
\$\endgroup\$
6
\$\begingroup\$

Pyth, (削除) 7 (削除ここまで) 6 bytes

mi2-sQ

-1 Byte thanks to @KZhang

Outputs 1 for odd, 2 for even.

Try it!

Explanation

m%-sQd2
m Q # For each element in the (implicit) input list
 sQ # Take the sum of all the elements
 - d # subtract that element, so that we now have the sum of the other elements
 % 2 # modulo 2; 1=off, 0=even
answered May 2, 2017 at 21:05
\$\endgroup\$
1
  • \$\begingroup\$ By changing the modulo %_2 to a GCD i2_, you can even make the d implicit changing the code to mi2-sQ, saving a byte. The outputs are changed to 2 for even, and 1 for odd. \$\endgroup\$ Commented May 3, 2017 at 0:35
6
\$\begingroup\$

05AB1E (legacy), (削除) 4 (削除ここまで) 3 bytes

O^È

Try it online!

O # Sum
 ^ # XOR with (implicit) input
 È # Print 1 for even / 0 for odd
answered May 2, 2017 at 18:34
\$\endgroup\$
2
  • \$\begingroup\$ 1 byte off \$\endgroup\$ Commented May 2, 2017 at 18:39
  • \$\begingroup\$ I know it's been a while, but you can remove the initial D, since input is implicitly. \$\endgroup\$ Commented May 31, 2019 at 13:18
5
\$\begingroup\$

Python 2, (削除) 33 (削除ここまで) 31 bytes

-2 bytes thanks to Leaky Nun

lambda x:[sum(x)-z&1for z in x]

Try it online!

answered May 2, 2017 at 18:34
\$\endgroup\$
2
  • \$\begingroup\$ 2 bytes off: sum(x)-z&1 instead of (sum(x)-z)%2 \$\endgroup\$ Commented May 2, 2017 at 18:35
  • 3
    \$\begingroup\$ sum(x,z)%2 would also work \$\endgroup\$ Commented May 2, 2017 at 19:19
5
\$\begingroup\$

R, 21 bytes

(sum(n<-scan())-n)%%2

reads the list from stdin and returns 0 for even, 1 for odd. binds the input to the variable n inside the call to sum instead of calling it outside, i.e., n=scan();(sum(n)-n)%%2

Try it online!

answered May 2, 2017 at 18:42
\$\endgroup\$
4
\$\begingroup\$

J, 6 bytes

2|]++/

Try it online!

answered May 3, 2017 at 4:17
\$\endgroup\$
3
\$\begingroup\$

Mathematica, 13 bytes

#+Tr@#&/*OddQ

or

OddQ[#+Tr@#]&
answered May 2, 2017 at 19:23
\$\endgroup\$
3
\$\begingroup\$

Clojure, 30 bytes

#(for[i %](odd?(apply - i %)))

Substracts all values from each value in turn, for example with input [a b c d] the 2nd calculated value is b - a - b - c - d = -(a + c + d). Output is false for even and true for odd.

But you might as well use + and calculate each subsequent term twice so it doesn't affect the parity.

answered May 2, 2017 at 20:09
\$\endgroup\$
3
\$\begingroup\$

CJam, 10 bytes

{_:+f+1f&}

This is an anonymous block (function) that takes the input from the stack and replaces it by the output.

Try it online!

Explanation

Consider input [1 2 3 1].

{ e# Begin block
 e# STACK: [1 2 3 1]
 _ e# Duplicate
 e# STACK: [1 2 3 1], [1 2 3 1]
 :+ e# Fold addition over the array: compute its sum
 e# STACK: [1 2 3 1], 7 
 f+ e# Map addition over the array with extra parameter
 e# STACK: [8 10 11 8]
 1 e# Push 1
 e# STACK: [8 10 11 8], 1
 f& e# Map bit-wise "and" over the array with extra parameter
 e# STACK: [0 0 1 0]
} e# End block
answered May 2, 2017 at 22:27
\$\endgroup\$
3
\$\begingroup\$

Retina, (削除) 40 (削除ここまで) 38 bytes

\d+
¶$` $'
^¶
\d+
$*
+` |11
%M`1
¶
 

Try it online! Outputs 1 for odd and 0 for even. Explanation: The first two lines duplicate the input once for each number in the input, but without the element itself. This creates an extra blank line which is then deleted. The input is then converted from decimal to unary, the spaces are deleted and the parity computed. Even parity is then converted to zero and the results joined back in to one line. Edit: Saved 2 bytes thanks to @FryAmTheEggman. I tried some other versions which are conceptually more pleasing but which take far too many bytes to express:

\d\B
T`2468O`00001
T`d`10`^([^1]*1[^1]*1)*[^1]*1[^1]*$

Changes all inputs to their parity, then flips all their parities if the total has odd parity.

\d+
$*
^.*
$&¶$&
 (?=.*$)
11
\B
0
T`d`10`.*¶1
¶0

Sums a duplicate of the input, then takes the parity of everything, then inverts the parities if the sum is odd, then deletes the sum again.

answered May 2, 2017 at 21:55
\$\endgroup\$
4
  • \$\begingroup\$ I tried what came to mind for me, and got a slightly shorter solution, though I still think it's hardly optimal. Particularly I don't like how I handle the extra zero I get at the end. \$\endgroup\$ Commented May 2, 2017 at 23:59
  • \$\begingroup\$ @FryAmTheEggman Your saving comes from having a less visible way of converting your ;s back to spaces. If you put the ; at the beginning then you can save a byte by deleting it immediately instead of after it converts to a 0. \$\endgroup\$ Commented May 3, 2017 at 0:19
  • \$\begingroup\$ Actually, looking again, why isn't your last stage just replacing newlines with spaces? Wouldn't that save 2 bytes? \$\endgroup\$ Commented May 3, 2017 at 2:02
  • \$\begingroup\$ @FryAmTheEggman Yes; I think I originally had more than one replacement to make in a previous iteration. \$\endgroup\$ Commented May 3, 2017 at 8:02
3
\$\begingroup\$

APL (Dyalog Unicode), (削除) 19 (削除ここまで) 7 bytes

Golfed 12 bytes thanks to @Adám

2|+/- ̈⊢

Try it online!

answered May 3, 2017 at 10:13
\$\endgroup\$
0
2
\$\begingroup\$

Japt, 7 bytes

£x +X&1

Try it online!

Explanation

 £ x +X&1
 mX{ x +X&1} // Ungolfed
UmX{Ux +X&1} // Variable introduction
UmX{ } // Replace each item in the input array with
 &1 // the parity of
 Ux // the sum of the input array
 +X // plus X.
 // Implicit: output result of last expression
answered May 2, 2017 at 18:36
\$\endgroup\$
2
\$\begingroup\$

Perl 5, 31 bytes

sub{map$x+=$_,@_;map$x-$_&1,@_}

Outputs 1 for odd and 0 for even.

answered May 2, 2017 at 21:42
\$\endgroup\$
4
  • \$\begingroup\$ +1, nice. I think this is 28 bytes, though: perldoc perlsub says, "The signature is part of a subroutine's body. Normally the body of a subroutine is simply a braced block of code.". \$\endgroup\$ Commented May 2, 2017 at 22:42
  • \$\begingroup\$ @msh210 Thanks! I don't think that's how it works, though- sure the body of the subroutine is only 28 bytes, but you can't leave out sub without breaking it. \$\endgroup\$ Commented May 3, 2017 at 23:35
  • \$\begingroup\$ But sometimes a subroutine works without sub, e.g. after sort or grep or as an argument to another subroutine. This may be worth asking about on Code Golf Meta. \$\endgroup\$ Commented May 4, 2017 at 4:22
  • \$\begingroup\$ @msh210 A subroutine works without sub only if it is used in a prototyped function (sort and grep are more or less prototyped). But otherwise, the sub is required. Regardless, golfing 3 bytes by omitting sub isn't really interesting. \$\endgroup\$ Commented May 4, 2017 at 12:16
2
\$\begingroup\$

Clojure(Script), 36 bytes

Output is true for odd and false for even. Both output and input are sequences.

(fn[l](map #(odd?(-(apply + l)%))l))
answered May 3, 2017 at 3:16
\$\endgroup\$
2
\$\begingroup\$

PHP, 50 Bytes

Online Versions

1 for odd , 0 for even

Output as string separated with _

<?foreach($_GET as$v)echo array_sum($_GET)-$v&1,_;

PHP, 72 Bytes

Output as array use array_map

<?print_r(array_map(function($v){return array_sum($_GET)-$v&1;},$_GET));
answered May 2, 2017 at 20:04
\$\endgroup\$
1
  • 1
    \$\begingroup\$ You can drop the ?:0 it doesn't do anything. foreach($_GET as$v)echo array_sum($_GET)-$v&1,_; \$\endgroup\$ Commented May 3, 2017 at 6:27
2
\$\begingroup\$

C, (削除) 68 (削除ここまで) 62 bytes

i;s;f(c,l)int*l;{while(i<c)s+=l[i++];while(i)l[--i]=s-l[i]&1;}

1 for odd, 0 for even

Detailed Try Online

f(int c, int * l)
{
 int i = 0, s = 0;
 while(i < c)
 {
 s = s + l[i];
 i = i + 1;
 }
 // assert(i == c)
 while(i > 0)
 {
 i = i - 1;
 l[i] = s - l[i]&1;
 }
}
answered May 2, 2017 at 22:33
\$\endgroup\$
2
\$\begingroup\$

Ohm, 4 bytes

DΣ-é

Try it online!

Basically a direct port of the MATL and 05AB1E answers. Uses true for even and false for odd.

answered May 3, 2017 at 3:05
\$\endgroup\$
2
\$\begingroup\$

Julia 1.0, 17 bytes

x->(sum(x).-x).%2

Try it online!

answered May 31, 2019 at 18:08
\$\endgroup\$
2
\$\begingroup\$

Fig, \4ドル\log_{256}(96)\approx\$ 3.292 bytes

%2-S

1 for odd, 0 for even.

Try it online!

%2-S
 S # sum
 - # minus each item of input array
%2 # mod each by 2
answered Feb 23, 2023 at 0:35
\$\endgroup\$
3
  • \$\begingroup\$ %2 can be replaced with O \$\endgroup\$ Commented Feb 28, 2023 at 20:30
  • \$\begingroup\$ @Seggan -S yields a list, O would join that list by nothing, unless it has been updated. \$\endgroup\$ Commented Feb 28, 2023 at 20:34
  • 1
    \$\begingroup\$ oops, true. never mind \$\endgroup\$ Commented Feb 28, 2023 at 20:35
1
\$\begingroup\$

k, 9 bytes

{2!x-+/x}

The output is a 1 for odd, and a 0 for even. Try it online.

Converted to pseudocode, it would be:

for number in x:
 yield (number - sum(x)) % 2
answered May 2, 2017 at 21:14
\$\endgroup\$
1
\$\begingroup\$

Scala, 19 bytes

x=>x.map(x.sum-_&1)

Try it online!

answered May 2, 2017 at 23:45
\$\endgroup\$
1
\$\begingroup\$

Brain-Flak, (削除) 94 (削除ここまで) (削除) 68 (削除ここまで) 66 bytes

({({}<>)<>})<>{<>(({})<({}<>{}<>(())){({}[()]<([{}])>)}{}>)<>}<>{}

Try it online!

This seems a little long for the task. There might be a more convenient way to do this.

Explanation

First we calculate the sum of the stack with:

({({}<>)<>})

The we go through the entire stack adding that result to each element and determine the pairity

<>{<>(({})<({}<>{}<>(())){({}[()]<([{}])>)}{}>)<>}<>{}

This uses a pretty cool mod 2 algorithm I came up with for this challenge.

({}(())){({}[()]<([{}]())>)}{}

This pushes 1 under the input decrements until the input reaches zero each time performing 1-n to the 1 we placed earlier, it then removes the input.

answered May 3, 2017 at 3:56
\$\endgroup\$
2
  • \$\begingroup\$ You can mod 2 at the last. You don't need to mod 2 the sum. \$\endgroup\$ Commented May 3, 2017 at 3:57
  • \$\begingroup\$ @LeakyNun Thanks! I just realized that and made the fix. \$\endgroup\$ Commented May 3, 2017 at 3:59
1
\$\begingroup\$

Wise, (削除) 54 (削除ここまで) 52 bytes

::^:??[:!^:?^:!^:?^?]|!::^??[!:?^:><^!:?^:!^:?^?]!&|

Try it online!

Explanation

This code would be a lot shorter if it didn't take so many bytes to swap the top two elements. The current record is

:?^:!^:?^!

This unfortunately constitutes the majority of the code.


First we take the XOR sum of the stack

::^:??[:!^:?^:!^:?^?]|!

We then XOR this with every element and the element with it last bit zeroed

::^??[!:?^:><^!:?^:!^:?^?]!&|
answered May 3, 2017 at 5:12
\$\endgroup\$
1
\$\begingroup\$

Java, (削除) 81 (削除ここまで) 78 bytes

3 bytes thanks to Kevin Cruissen

void f(int[]a){int s=0,i=a.length;for(int x:a)s+=x;for(;i-->0;a[i]=s-a[i]&1);}

Try it online!

Modifies the array in-place.

answered May 3, 2017 at 2:56
\$\endgroup\$
3
1
\$\begingroup\$

AWK, 64 bytes

{for(i=0;++i<=NF;print s[i]%2)for(j=0;++j<=NF;)if(i!=j)s[i]+=$j}

Try it online!

Outputs a 0 for even sums and 1 for odd sums separated by newlines. The only even slightly out-of-the-box thinking was placing the print command inside the for "increment" step. I tried a few "clever" ways to print, but they didn't save bytes.

Just for giggles, if you don't want newlines:

{for(i=0;++i<=NF;m=m,s[i]%2)for(j=0;++j<=NF;)if(i!=j)s[i]+=$j}1

which has the same byte-count as above, but is a bit more obtuse.

answered May 3, 2017 at 18:54
\$\endgroup\$
1
\$\begingroup\$

Swift - 55 bytes

Finally beats C! Also, 0 for even, 1 for odd

func g(a:[Int]){for i in a{print((a.reduce(0,+)-i)%2)}}

A function, with usage: g(a: [1,2,3,2,1] // => 0 1 0 1 0

Check it out!

answered May 3, 2017 at 6:11
\$\endgroup\$
2
  • \$\begingroup\$ Not familiar with Swift, but in many languages you can replace (x-y)%2 with x-y&1 \$\endgroup\$ Commented May 4, 2017 at 1:57
  • \$\begingroup\$ @Cyoce For me, after testing, it doesn't work. Bitwise operations aren't Swift's forte \$\endgroup\$ Commented May 4, 2017 at 4:43
1
2

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.