You will be given two arrays of floating-point numbers. Your task is to pair the corresponding elements of the two arrays, and get the maximum of each pair. However, if the two corresponding elements are equal, you must take their sum instead.

For example, given the lists [1, 3, 3.2, 2.3] and [3, 1, 3.2, 2.6], you must do the following:

• Pair the elements (or zip): [[1, 3], [3, 1], [3.2, 3.2], [2.3, 2.6]].

• Go through each pair and apply the process above: [3, 3, 6.4, 2.6].

## Specs

• The arrays / lists will always have equal length. They may however be empty.

• The numbers they contain will always fit your language's capabilities, as long as you do not abuse that. They may be positive, zero or negative, you must handle all types.

• If it helps you reduce your byte count, you may also take the length of the lists as input.

# Test Cases

Array_1, Array_2 -> Output

[], [] -> []
[1, 2, 3], [1, 3, 2] -> [2, 3, 3]
[1, 3, 3.2, 2.3], [3, 1, 3.2, 2.6] -> [3, 3, 6.4, 2.6]
[1,2,3,4,5,5,7,8,9,10], [10,9,8,7,6,5,4,3,2,1] -> [10, 9, 8, 7, 6, 10, 7, 8, 9, 10]
[-3.2, -3.2, -2.4, 7, -10.1], [100, -3.2, 2.4, -7, -10.1] -> [100, -6.4, 2.4, 7, -20.2]

• You say that the numbers will always fit "within your language's" capabilities". As long as you do not "abuse" that. Would only supporting integers in a language that does not have floats be considered an abuse? The question does say floating point but I don't really see a reason why it has to be floats. The same process can be done on integers. I would like to solve this in Brain-Flak but Brain-flak only supports ints. Aug 28, 2017 at 17:03
• @WheatWizard I can make an exception for that. Go ahead and post your answer and mention I allowed it to avoid confusion.
– user70974
Aug 28, 2017 at 18:06

# Kotlin, 7875716665 59 bytes

It's my first attempt, be cool :D

a.zip(b).map{(a,b)->when{b>a->b;a>b->a;else->a*2}}.toList()


TIO doesn't work with this solution (and i don't know why), source code for testing below

fun main(args: Array<String>) {
bestOfTwo(floatArrayOf(), floatArrayOf()).print()
bestOfTwo(floatArrayOf(0F), floatArrayOf(0F)).print()
bestOfTwo(floatArrayOf(1F,2F,3F), floatArrayOf(1F,3F,2F)).print()
bestOfTwo(floatArrayOf(1F,3F,3.2F,2.3F), floatArrayOf(3F,1F,3.2F,2.6F)).print()
bestOfTwo(floatArrayOf(1F,2F,3F,4F,5F,5F,7F,8F,9F,10F), floatArrayOf(10F,9F,8F,7F,6F,5F,4F,3F,2F,1F)).print()
bestOfTwo(floatArrayOf(-3.2F,-3.2F,-2.4F,7F,-10.1F), floatArrayOf(100F,-3.2F,2.4F,-7F,-10.1F)).print()
}

fun bestOfTwo(a :FloatArray, b :FloatArray): List<Float> =
a.zip(b).map{(a,b)->when{b>a->b;a>b->a;else->a*2}}.toList()

fun List<Float>.print() {
this.forEach { print("$it, ") }; println("") }  EDIT: -3 by replace "a+b[i]" by "a*2" -4 by replace "mapIndexed" method by "zip" (Thanks to @AnonymousReality Swift solution) -5 by replace "Math.max" method by when condition -1 by change when condition order -6 by change toFloatArray() by toList() • Welcome to PPCG! Please don't be discouraged by the downvote (it's the result of a slight quirk of the system that happens when a new user's first post is auto-flagged for quality and then they improve said post!!) Aug 28, 2017 at 13:23 • The worst "feature" ever...btw don't feel bad about it. Aug 28, 2017 at 13:26 # Python 2, 45 bytes A mix of my initial solution and @ovs'. lambda*a:map(lambda x,y:max(x,y)*-~(x==y),*a)  Try it online! # Python 2, 49 bytes lambda x,y:[max(a,b)*-~(a==b)for a,b in zip(x,y)]  Try it online! # Python 2, 46 bytes @ovs suggested this method to save 3 bytes. lambda*x:[max(a,b)*-~(a==b)for a,b in zip(*x)]  Try it online! # How? First off, we pair the corresponding elements, by using either * or zip(). That allows us to do our further golfing by working either with a map or a list comprehension. The cool trick in this answer is this part: max(x,y)*-~(x==y). How does that work? - Well, as most of you already know, Python auto-converts bool values to integers when they are used in arithmetic operations. Hence, (x==y) gets evaluated as 1, if the condition is met. However, if the two values are not equal, it returns 0 instead. Then, the bitwise operation -~ increments the value returned from the bool by 1, giving us either 2 or 1. max(a,b) gives the maximum value of the pair and * multiplies it by the value returned above (so it gets multiplied by 2 only if they are equal, in which case max() returns the value of both). This is based on the fact that the sum of two equal numbers is in fact either of them doubled, and kind of "abuses" Python's bool class being a subclass of int. • Wow, that was really fast! – user70974 Aug 28, 2017 at 12:05 • more straightforward, same number of bytes: lambda*a:map(lambda x,y:(x<=y)*y+(x>=y)*x,*a) Aug 28, 2017 at 20:09 • @jferard I fact, that's already Luis' solution. Aug 28, 2017 at 20:12 • @Mr.Xcoder Oops! I didn't read the whole page... Aug 28, 2017 at 20:18 • Never say "above," as the ordering can change (I don't see your solution above) Aug 28, 2017 at 21:53 # JavaScript (ES6), 534945 43 bytes a=>b=>a.map((x,y)=>(y=b[y])>x?y:y<x?x:x+y)  • 4 bytes saved by borrowing a trick from Mr. Xcoder. • 2 bytes saved thanks to Arnauld. ## Try it o.innerText=(f= a=>b=>a.map((x,y)=>(y=b[y])>x?y:y<x?x:x+y) )(i.value=[1,3,3.2,2.3])(j.value=[3,1,3.2,2.6]);oninput=_=>o.innerText=f(i.value.split,.map(eval))(j.value.split,.map(eval)) <input id=i><input id=j><pre id=o> ## Explanation a=>b=>  Anonymous function taking the 2 arrays as arguments via parameters a and b, in currying syntax (i.e., call with f(a)(b) a.map((x,y)=> )  Map over the first array, passing each element through a function where x is the current element and y is the current index. (y=b[y])  Get the element at index y in the second array and assign that as the new value of y. >x?y  Check if y is greater than x and, if so, return y. :y<x?x  Else, check if y is less than x and, if so, return x :x+y  Else, return the sum of x and y. (Multiplying x or y by 2 would also work here, for the same number of bytes.) • j.value.split,.map(eval) or eval('['+j.value+']')? Also x+y would look neater IMHO. – Neil Aug 28, 2017 at 14:56 • @Neil: 1) I find the former easier to type. Also, I have a couple of Snippet templates on one of my machines; it's easier just to tack .map(eval) onto them. 2) Agreed, will edit in momentarily. Aug 28, 2017 at 16:13 # Jelly, 4 bytes =‘×»  Try it online! This uses the exact same approach as my APL answer, except Jelly has a builtin for adding one to a number! • Hate to be a spoilsport, but aren't some of those characters more than one byte each in any sensible encoding? Aug 30, 2017 at 1:26 • This uses the jelly codepage. Aug 30, 2017 at 1:37 • I finally won against competition! Sep 7, 2017 at 0:39 • @Zacharý ONE MAN, 4 btytes... THIS SUMMER... You... WILL... BE... JELLY OF HIM... rated J for Jelly. Oct 27, 2017 at 18:58 # Python 3, 4846 44 bytes -2 bytes thanks to @nwellnhof lambda*a:map(lambda*x:max(x)*2/len({*x}),*a)  Try it online! # Haskell, 34 bytes x!y|x>y=x|x<y=y|1<2=x+y zipWith(!)  Try it online. • Same byte count: x!y=max x y+sum[x|x==y]. – nimi Aug 28, 2017 at 19:46 # R, 31 29 bytes function(a,b)pmax(a,b)+a*!a-b  pmax takes the parallel maximum of the two (or more) arrays (recycling the shorter as needed). I was looking at Luis Mendo's comment and obviously I realized the approach could work for R as well. That got me to 30 bytes, but then I started playing around with different ways of getting indices instead to improve my original answer, and stumbled upon !a-b as TRUE where a==b and FALSE otherwise, equivalent to a==b. However, for whatever reason, R doesn't require parentheses around !a-b as it does for a==b, which saved me two bytes. As mentioned by JDL in the comments, this works because ! (negation) has lower precedence than the binary operator - in R, which is strange. Try it online! (new version) Try it online! (original) • It turns out that unary "!" has lower precedence in R than binary "-", which I think is quite unusual (and I hadn't realised until reading this answer!) – JDL Aug 29, 2017 at 15:29 • @JDL yeah I almost always have to open up the R Syntax page while golfing in case of weird quirks like this...and also because I can never remember the precedence of : when interacting with arithmetic. Aug 29, 2017 at 15:55 # Dyalog APL, 5 bytes ⌈×1+=  Try it online! ## How? • ⌈, element-wise maximum of the arguments • ×, element-wise multiply • 1+=, 1 added to the element-wise equality of the arguments This works because if the numbers are unequal, 1+= will be 1, which when multiplied by the maximum, is the maximum. When the numbers are equal, 1+= will return 2, when that is multiplied by the maximum, we get twice the maximum, or the maximum added to itself. # Jelly, 6 bytes żSṀE?€  A dyadic link taking a list of numbers on each side and returning the resulting list. Try it online! or see a test-suite*. ### How? żSṀE?€ - Link: list of numbers L, list of numbers R e.g. [1,3,3.2,2.3], [3,1,3.2,2.6] ż - zip - interleave L & R [[1,3],[3,1],[3.2,3.2],[2.3,2.6]] € - for each pair: ? - { if: E - ...condition: equal 0 0 1 0 S - ...then: sum 6.4 Ṁ - ...else: maximum 3 3 2.6 - } ... -> [3 ,3 ,6.4 ,2.6]  An alternative is this monadic link taking a list of the two lists, also 6 bytes: +»⁼?"/  * I don't think I've ever created a test-suite footer almost three times the byte count of the code before! • Outgolfed!. +1 for the practically verbatim interpretation of the question. Aug 28, 2017 at 20:47 • ...and I've been caught out be forgetting that » vectorises before! Aug 28, 2017 at 20:55 • What else would it do, take the maximum array in some convoluted way? Aug 28, 2017 at 21:13 • No need for any convoluted definitions, Python manages - for example max([1,1,0],[1,0,3]) -> [1,1,0] (not [1,1,3]). Aug 28, 2017 at 21:20 • So, basically infinite-base? Aug 28, 2017 at 21:21 # 05AB1E, 5 bytes øεMÃO  Try it online! -1 thanks to Emigna. • Nice idea using γ! Aug 28, 2017 at 13:20 • @Emigna I really wanted "maximal elements", and {γθ is probably the shortest I can get to for that. Aug 28, 2017 at 13:21 • How about øεMÃO? Aug 28, 2017 at 13:23 • @Emigna Cool, thanks! (duh why didn't I think of MÃ) yay got the lead now :p btw øεZÃO would work too Aug 28, 2017 at 13:23 # MATL, 7 bytes X>tG=s*  Input is a two-row matrix, where each row is one of the arrays. Try it online! ### Explanation X> % Implicit input. Maximum of each column t % Duplicate G % Push input = % Is equal? Element-wise with broadcast. Gives a two-row matrix s % Sum of each column. Gives a row vector containing 1 and 2 * % Multiply, element-wise. Implicit display  # Java 8, 806967666564 63 bytes (a,b,l)->{for(;l-->0;)if(a[l]>=b[l])b[l]=a[l]*(a[l]>b[l]?1:2);}  Modifies the second input-array instead or returning a new float-array to save bytes. -11 bytes by taking the length as additional integer-input, which is allowed according to the challenge rules. -5 bytes thanks to @OliverGrégoire (one byte at a time.. xD) -1 byte indirectly thanks to @Shaggy's JS answer, by using a[l]*2 instead of a[l]+b[l]. Explanation: Try it here. (a,b,l)->{ // Method with 2 float-array and integer parameters and no return-type for(;l-->0;) // Loop over the array if(a[l]>=b[l]) // If the current value in a is larger or equal to b: b[l]= // Modify the second input-array: a[l]* // Use a multiplied by: (a[l]>b[l]? // If the current value in a is larger than b: 1 // Multiply by 1 : // Else (a is smaller of equal to b): 2) // Multiply by 2 // End of loop (implicit / single-line body) } // End of method  • "If it helps you reduce your byte count, you may also take the length of the lists as input." It will definitely reduce your byte-count ;) Aug 28, 2017 at 12:18 • Also, 2 bytes shorter: a->b->l->{float A,B;for(;l-->0;b[l]=(A=a[l])<B?B:A>B?A:A+B)B=b[l];} Aug 28, 2017 at 12:25 • And you can save one more byte by putting float A, B in the for initialization. Aug 28, 2017 at 12:28 • Or this: (a,b,l)->{for(;l-->0;)if(a[l]>=b[l])b[l]=a[l]*(a[l]>b[l]?1:2);} (63 bytes) Aug 28, 2017 at 12:44 • @OlivierGrégoire Lol.. with golfing every byte helps, but that doesn't mean you need to golf it one byte at a time. ;p Aug 28, 2017 at 12:53 # Pyth, 11 bytes m*eSdhnd{dC  Try it here! # Pyth, 12 bytes m*eSdhqhdedC  Try it here! or m*eSdh!tl{dC  Try it here! • @Jakube That's Erik's solution already, sadly. I wanted to use that too, but I can't now Aug 29, 2017 at 13:56 • Oh, didn't see that one. Aug 29, 2017 at 13:57 # 05AB1E, 98 7 bytes Saved a byte as Erik the Outgolfer pointed out that a list of lists is valid input. øεMsËi·  Try it online! Explanation ø # zip the lists ε # apply to each pair M # get max s # swap the top 2 elements on the stack Ëi # if all elements are equal · # double the max  • Wow, that was really fast! – user70974 Aug 28, 2017 at 12:05 • You can save a byte by removing the ‚ and inputting as a pair of a list and a list. Aug 28, 2017 at 13:01 • @EriktheOutgolfer: True. I assumed we weren't allowed to, but I see that the challenge does specify standard I/O rules. Thanks for notifying :) Aug 28, 2017 at 13:16 • @Emigna Tip: don't make rules out of your mind ;) Aug 28, 2017 at 13:19 • @EriktheOutgolfer: Yeah I really need to stop doing that. Especially rules which make my programs longer ;) Aug 28, 2017 at 13:19 # Mathematica, 31 bytes MapThread[If[#==#2,2#,Max@##]&]  # J, 7 bytes >.+@.=  Try it online! Takes one list as the left argument and the other as the right. Luckily, equality is a rank zero operation. # Explanation >.+@.= = Compare equality pairwise @. If equal + Sum >. (Else) take greater value  @. isn't really an if statement, but in this case it functions as one (it indexes into the gerund >.+ based on the result of its right argument and applies that to the input). • Nice job, I know I couldn't do this, even though you have beenoutgolfed by my translation of my APL. >_< Aug 29, 2017 at 0:15 • J really shines here Aug 29, 2017 at 8:50 • @Zacharý rats, well-golfed nonetheless. – cole Aug 29, 2017 at 13:49 # Ruby, 42 bytes ->a,b{a.zip(b).map{|x,y|[x+y,x,y][x<=>y]}}  Try it online! The spaceship operator is great. # TI-Basic, 23 21 bytes Prompt A,B (ʟA=ʟB)ʟA+max(ʟA,ʟB  Too bad lists take up two bytes each... • You can save two bytes by prompting for X and Y, then using ʟX and ʟY to access them, i.e. "Prompt X,Y:ʟX(ʟX=ʟY)+max(ʟ1,ʟ2". Aug 29, 2017 at 21:53 • Also, this is currently invalid, since L1(L1=L2) attempts to get the element of L1 at a list, which throws an error. To fix that, swap the order, i.e. (L1=L2)L1. Aug 29, 2017 at 21:58 • @ScottMilner Thanks for pointing both of those out. Aug 29, 2017 at 23:04 # Octave, 36 Byte @(a,b)a.*(a>b)+b.*(b>a)+2.*a.*(a==b)  # Pyth, 7 bytes ms.MZdC  Try it here. # Python 3, 4946 45 bytes 3 bytes removed thanks to @Mr.Xcoder (splat instead of two arguments), and 1 byte thanks to @ovs (map instead of list comprehension) lambda*x:map(lambda a,b:a*(a>=b)+b*(b>=a),*x)  Try it online! • 46 bytes: lambda*c:[a*(a>=b)+b*(b>=a)for a,b in zip(*c)]. Turns out this is quite good too :) - Too bad there is no place for further golfing Aug 28, 2017 at 12:43 • @Mr.Xcoder Thanks! Good idea! Aug 28, 2017 at 12:47 • 45 bytes by using map instead of zip. – ovs Aug 28, 2017 at 17:35 # Common Lisp, 60 59 bytes (mapcar(lambda(x y)(*(max x y)(if(= x y)2 1)))(read)(read))  Try it online! -1 byte thanks to @Zacharý! • 59 bytes: (mapcar(lambda(x y)(*(max x y)(if(= x y)2 1)))(read)(read)). Aug 29, 2017 at 0:42 • You're welcome, I don't know lisp that well, I just translated my other answers into Lisp which ended up saving a byte. Aug 29, 2017 at 8:11 # Python with numpy, 28 bytes lambda a,b:a*(a>=b)+b*(b>=a)  Assumes input is given as two numpy arrays. • If we are using numpy then here is my worse solution: lambda a,b:n.fmax(a,b)*((a==b)+1) Aug 29, 2017 at 18:09 • @Erich I like the idea, but to do that I would need to import numpy as n. I get away without it here because it's implicit in the input. – user48543 Aug 29, 2017 at 18:22 • I guess i'm a bit shaky on the explicit byte counting, often python answers are simply lambdas, when an actual implementation of an answer would require assigning it to something. for this reason I wonder if it is allowable to get away with an implicit import statement as well? Aug 29, 2017 at 18:32 • @Erich In general, you can only refer to a variable n if you've defined n in your code, so imports must be explicit. By default, we allow functions or full programs as answers, which includes anonymous functions. – user48543 Aug 29, 2017 at 18:39 • Well, this only needs input as numpy arrays, rather than importing numpy. But does this even work without using return? Aug 29, 2017 at 20:29 # C# (.NET Core), using Linq 47+18=65 bytes x=>y=>x.Zip(y,(a,b)=>a>b?a:b>a?b:b+a).ToArray()  Try it online! # C# (.NET Core), 82 bytes x=>y=>l=>{for(int i=0;i<l;i++)x[i]=x[i]>y[i]?x[i]:y[i]>x[i]?y[i]:y[i]*2;return x;}  Try it online! • You can drop the LINQ answer by a few bytes by changing namespace System.LINQ to using System.LINQ Aug 28, 2017 at 18:27 • @jkelm yeah, I've been wondering if the 'using System; is to be included or not like that, I guess not. I'll clean it up Aug 28, 2017 at 19:23 • System.Linq is included in the "Visual C# Interactive Compiler". I am not totally sure about returning Array vs IList vs IEnumerable, but if all are eligible then you can get the byte count to 37 - tio.run/##Sy7WTS7O/… – dana Dec 17, 2018 at 4:22 # Perl 6, 34 28 bytes {map {.max*2/set$_},[Z] $_}  Try it online! # Swift 3, 81 79 Bytes func n(a:[Double],b:[Double]){for(x,y)in zip(a,b){print((x==y) ?x+y:max(x,y))}}  Swift has an interesting property in that an Int isn't directly castable to a Double, so you have to specify any arrays as being arrays of Doubles before passing them to the function. (e.g.) var k:[Double] = [1,2,3,4,5,5,7,8,9,10] Edit: -2 bytes thanks to @EriktheOutgolfer • Do you need spaces around (x,y) and before ?? Aug 28, 2017 at 13:28 • @EriktheOutgolfer The one before ? is needed because Swift would treat them as optional types instead of ternaries (which they aren't). The others aren't. Apart from that, this can be drastically golfed. – user70974 Aug 28, 2017 at 13:31 • @EriktheOutgolfer - TheIOSCoder has already answered you partly, but you're right, you don't need the ones in the for loop, interesting! Aug 28, 2017 at 13:34 • 73 bytes: func n(a:[Float],b:[Float]){print(zip(a,b).map{$0==$1 ?2*$0:max($0,$1)})} (the float inaccuracies need not to be handled by default) Aug 28, 2017 at 13:38
• Or 74 bytes: func n(a:[Float],b:[Float]){print(zip(a,b).map{($0==$1 ?2:1)*max($0,$1)})} Aug 28, 2017 at 13:40

# C, 76 75 bytes

Thanks to @Kevin Cruijssen for saving a byte!

f(a,b,n)float*a,*b;{for(;n--;++a,++b)printf("%f ",*a>*b?*a:*b>*a?*b:*a*2);}


Try it online!

# Japt, 13 bytes

íV,È¥Y Ä *XwY


Try it online! with the -Q flag to format the output array.

• Nicely done. I made 2 attempts at this earlier with both coming out at 17 bytes. I'd forgotten í could take a function as a second argument. Aug 28, 2017 at 16:39

# Rust, 107 97 bytes

|a:V,b:V|a.iter().zip(b).map(|(&x,y)|if x==y{x+y}else{x.max(y)}).collect::<V>();
type V=Vec<f32>;


Try it online!

Saved 8 bytes thanks to @mgc

• I guess you can save 8 bytes by using type inference on the collected Vec and by using the max method of f32s: |a:Vec<f32>,b:Vec<f32>|a.iter().zip(b).map(|(&x,y)|if x==y{x+y}else{x.max(y)}).collect::<Vec<_>>();
– mgc
Aug 28, 2017 at 22:26
• @mgc Thanks! Type inference was a good idea, but in this case type alias is even shorter. Aug 29, 2017 at 4:41

# Scala, 61 Bytes

x=>y=>x zip y map(v=>if(v._1==v._2)v._1*2 else v._1 max v._2)


The above is a function literal in Scala. Here's an explanation.

x=>y=>         // Function literal taking 2 * Vector[Float], x and y.
x zip y        // Zip x and y into one list of pairs.
map(           // Replace every element in the list via a function.
v=>            // Function literal that takes a pair of floats.
if(v._1==v._2) // If the pair are equal.
v._1*2         // Set the element to the first member of the pair multiplied by 2.
else           // Otherwise.
v._1 max v._2) // Set it to their max.