Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Minimum Scalar Product

The inspiration for this code golf problem is from Google's code jam competition. The premise behind the problem is, given the input of two vectors of varying lengths, find the minimum possible scalar. A scalar can be found using the following formula:

x1 * y1 + x2 * y2 + ... + xn * yn

The problem, however, is that multiple values for the scalar can be found depending on the order of the numerals in the input case (seen below). Your goal is to determine the minimum possible scalar integer solution by plugging in the input case numbers into the equation and solving for it. You may use every number in the input only once, and must use all of the numbers.

Allow me to provide an example with the following vectors.

Input

3
1 3 -5
-2 4 1

Output

-25

The first integer on the line represents the number of numbers, n, in each vector. In this case, we have three numbers in each vector.

The number n may vary with each test case, but there will always be two vectors.

In the example input, the minimum scalar product would be -25.

(-5 * 4) + (1 * 1) + (3 * -2) = 25

Rules

  • You may only use each integer in both vectors once.
  • You must use all integers in the vectors.
  • Your output must only include the final product
  • I'll select the solution with the least amount of code, which follows all of the specifications listed above, in any language!

Hint: You don't need to brute force this problem, unless it makes your code shorter. There is a specific method involved in finding the minimum spanning scalar :).

share|improve this question
    
I really don't want to spoil for anybody, so don't open this unless you already know the answer. this is so well-known it's funny. en.m.wikipedia.org/wiki/Rearrangement_inequality – proud haskeller Mar 13 at 8:15

11 Answers 11

up vote 6 down vote accepted

Jelly, 6 bytes

ṢṚ×Ṣ}S

Try it online!

Using brute force is equally short:

Œ!×S€Ṃ

How it works

ṢṚ×Ṣ}S  Main link. Arguments: u (vector), v (vector)

Ṣ       Sort the components of u.
 Ṛ      Reverse.
   Ṣ}   Sort the components of v.
  ×     Multiply the results, element by element.
     S  Compute the sum of the products.
share|improve this answer

MATL, 6 bytes

Code:

SiSP*s

My first MATL answer :)

Explanation:

S       # Sort the first array
 iS     # Take the second array and sort it
   P    # Flip the array
    *   # Multiply both arrays with each other
     s  # Sum of the result

Try it online!

share|improve this answer
1  
I'm glad to see this! :-) – Luis Mendo Mar 12 at 10:05

Seriously, 6 bytes

,SR,S*

Try it online!

Explanation:

,SR,S*
,SR     input first vector, sort, reverse
   ,S   input second vector, sort
     *  dot product
share|improve this answer

APL, 15 bytes

{+/⍺[⍒⍺]×⍵[⍋⍵]}

This is a dyadic function that accepts arrays on the left and right and returns an integer. It uses the same approach as my Julia answer: dot product of the sorted arrays, one descending and one ascending.

Try it here

share|improve this answer

Mathematica, 30 17 bytes

-13 bytes by murphy

Sort@#.-Sort@-#2&

Function, input is vector1(list),vector2(list) Several revisions:

Plus@@(Sort@#*Reverse@Sort@#2)&(*me*)
Total[Sort@#*Reverse@Sort@#2]& 
Sort@#.Reverse@Sort@#2&        (*alephalpha*)
Sort@#.Sort[#2,#>#2&]&         (*murphy*)
Sort@#.SortBy[#2,-#&]          (*me*)
Sort@#.-Sort@-#2&              (*murphy*)
share|improve this answer
    
clever solution! – baseman101 Mar 12 at 4:49
2  
Sort@#.Reverse@Sort@#2& – alephalpha Mar 12 at 6:36
    
Sort@#.Sort[#2,#>#2&]& – murphy Mar 12 at 21:21
1  
Sort@#.-Sort@-#2& – murphy Mar 12 at 21:33
    
Or for your solution 1, Sort@#.SortBy[#2,-#&] – CalculatorFeline Mar 12 at 21:45

Pyth - 14 8 bytes

I think I figured out the trick.

s*VSQ_SE

Try it online here.

share|improve this answer

Javascript ES6, 69 bytes

a=>b=>a.sort((x,y)=>x-y).map((x,y)=>i+=b.sort((x,y)=>y-x)[y]*x,i=0)|i

Wow, this is way too long.

share|improve this answer
    
I think trying to reuse the sort function is costing you 3 bytes. – Neil Mar 12 at 20:35
    
I did more golfing. Better? – Mama Fun Roll Mar 12 at 20:43
    
You can probably save a byte with |i instead of &&i – ETHproductions Mar 13 at 1:36
    
Thx @ETHproductions – Mama Fun Roll Mar 13 at 1:38
    
Yes, that's what I was thinking of. – Neil Mar 13 at 10:58

CJam, 11 Bytes

q~$\$W%.*:+

Try it online!

share|improve this answer

Julia, 32 25 bytes

x->y->-sort(-x)⋅sort(y)

This is an anonymous function that accepts two arrays and returns an integer. To call it, assign it to a variable and do f(x)(y).

For inputs x and y, we simply compute the dot product of x sorted in reverse order with y sorted. We get x in reverse sorted order by negating all values, sorting, then negating again.

Saved 7 bytes thanks to Dennis!

share|improve this answer

Perl 6, 33 30 bytes

{sum @^a.sort Z*@^b.sort.reverse}
share|improve this answer
    
Why not {sum @^a.sort Z*[R,] @^b.sort}((1,3,-5),(-2,4,1)).say – Aleks-Daniel Jakimenko-A. May 8 at 15:16

RETURN, 29 bytes

[{␆␃}\{␆}␄␅[¤¥][×␌]#}␁[¤][+]#]

Try it here.

Replace any ␆␃␄␇ with their unprintable counterparts.

Anonymous lambda that leaves result on stack2. Usage:

""{1 3 0 5-}""{0 2- 4 1}[{␆␃}\{␆}␄␅[¤¥][×␌]#}␁[¤][+]#]!

Explanation

[                                 ]  lambda
 {␆␃}                              sort and reverse first stack
       \{␆}                         sort second stack
            ␄␅                     transpose and flatten
               [  ][  ]#             while loop
                ¤¥                     check if 2 items exist in stack
                    ×                  if so, multiply top 2 items
                     ␌                 and push to stack2
                        }␁          switch to stack2
                           [¤][+]#   sum stack2
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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