23
\$\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.

\$\endgroup\$

34 Answers 34

1
2
0
\$\begingroup\$

Ruby, 47 33 31 Bytes

->a{a.map{|x|a.inject(:+)-x&1}}

It maps through each element, determining the parity of the sum of all elements minus the current element (thus making it 0).

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

C#, 142 121 bytes


Data

  • Input An array of 32-byte signed integers
  • Output An array of 32-byte signed integers where 0 represents even and 1 represents odd

Golfed

(int[] i)=>{var l=i.Length;var o=new int[l];var s=0;foreach(int x in i)s+=x;for(int x=0;x<l;x++)o[x]=s-i[x]&1;return o;};

Ungolfed

( int[] i ) => {
   var l = i.Length;
   var o = new int[ l ];
   var s = 0;
   
   foreach( int x in i )
      s += x;
   
   
   for( int x = 0; x < l; x++ )
      o[ x ] = s - i[ x ] & 1;
   
   return o;
};

Ungolfed readable

// Takes an array of Int32 objects ( 32-byte signed integers )
( int[] i ) => {
   // Save the length of the array to save on byte count
   var l = i.Length;
   
   // Initialize an array with the same size the the input array
   var o = new int[ l ];
   
   // Initialize a var to store the sum of all numbers
   var s = 0;
   
   // Cycle through every number from the input
   foreach( int x in i )
      
      // Sum all of them
      s += x;
   
   // Cycle again through every number
   for( int x = 0; x < l; x++ )
      
      // Subtracts the number for the sum
      //    and performs a bitwise and
      o[ x ] = s - i[ x ] & 1;
   
   // Return the array with odds and evens
   return o;
};

Full code

using System;
using System.Collections.Generic;

namespace Namespace {
   class Program {
      static void Main( String[] args ) {
         Func<Int32[], Int32[]> f = ( int[] i ) => {
            var l = i.Length;
            var o = new int[ l ];
            var s = 0;
            
            foreach( int x in i )
               s += x;
            
            
            for( int x = 0; x < l; x++ )
               o[ x ] = s - i[ x ] & 1;
            
            return o;
         };

         List<Int32[]>
            testCases = new List<Int32[]>() {
               new Int32[] { 1, 2, 3, 1 },
               new Int32[] { 1, 2, 3, 2, 1 },
               new Int32[] { 2, 2 },
               new Int32[] { 100, 1001 },
            };

         foreach( Int32[] testCase in testCases ) {
            Console.WriteLine( $" Input: {String.Join( ",", testCase )}\nOutput: {string.Join( ",", f( testCase ) )}\n" );
         }

         Console.ReadLine();
      }
   }
}

Releases

  • v1.1 - -21 bytes - Changed the output to an array of ints to reduce the byte count thanks to Arthur Rey's comment. Changed the ternary verification to a bitwise operation, thanks to Cyoce's suggestion
  • v1.0 - 142 bytes - Initial solution.

Notes

  • None
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ You can just have o[x]=(s-i[x])%2==0 instead of o[x]=(s-i[x])%2==0?"even":"odd" since you're allowed to return consistent values else than even and odd. \$\endgroup\$ – Arthur Rey May 3 '17 at 23:43
  • 1
    \$\begingroup\$ If I'm not mistaken instead of (s-i[x])%2 you can have s-i[x]&1 \$\endgroup\$ – Cyoce May 4 '17 at 1:55
0
\$\begingroup\$

Perl 6, 16 bytes

{(.sum X-$_)X%2}

Try it

This results in a list where 0 represents even and 1 represents odd.

Expanded:

{  # bare block lambda with implicit parameter $_
  (
    .sum  # the sum of the input array
    X[-]  # crossed using &infix:« - »
    $_    # the input

    # this produces the sum of the other elements

  )
  X[%]    # cross that using &infix:« % » the modulus operator
  2       # with 2
}
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Japt, 6 bytes

®nUx)v

Try it

®nUx)v     :Implicit input of array U
®          :Map
 n         :Subtract from
  Ux       :  Sum of U
    )      :End subtraction
     v     :Divisible by 2?
| improve this answer | |
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.