Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

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

Challenge

Given an array of integers, received from stdin, function arguments, program arguments, or some other method:

Output only the minimum and maximum numbers in the array, through a return value, stdout, or other fitting methods.

Example session

> minmax( {0, 15, 2, 3, 7, 18, -2, 9, 6, -5, 3, 8, 9, -14} )
-14 18

Reference implementation

// C++

void minmax(std::vector<int> v) {
    int min = v[0]; int max = v[0];
    for(std::vector<int>::iterator it = v.begin(); it != v.end(); it++) {
        if (*it < min)
            min = *it;
        if (*it > max)
            max = *it;
    }
    std::cout << min << ' ' << max << std::endl;
}

Rules

  • You may not use a built-in function to calculate the values.
  • Standard loopholes disallowed.
  • Creative implementations encouraged.
  • This is , shortest answer wins but will not be selected.

Clarifications

  • If the array contains 1 element you need to output it twice.
  • If the minimum and maximum values are the same, you need to output them both.
share|improve this question
    
Anti-Dupe Information: Built ins are explicitly disallowed, and there are no strings in the array – Dmitry Kudriavtsev 1 hour ago
    
Is it allowed to output a single value if min and max are the same or do we have to output the same value twice? – Martin Ender 1 hour ago
    
@MartinEnder Same value twice. – Dmitry Kudriavtsev 1 hour ago
3  
This is a do X without Y challenge, which aren't particularly interesting. – Mego 1 hour ago
4  
@DmitryKudriavtsev Try the sandbox next time. – Mego 1 hour ago

16 Answers 16

Jelly, 3 bytes

Ṣ.ị

Try it online!

Sort the array, and then takes the 0.5-th element.

Jelly uses 1-indexing, and floating points indexing means take its floor and its ceil.

So the 0.5-th element would give you the 0th element and the 1st element.

The 0th element is the last element.

share|improve this answer
    
Pretty Clever I'm wait for it.... Jelly! – Rohan Jhunjhunwala 6 mins ago

Python, 61 bytes

lambda s:sorted(s)[::len(s)-(len(s)>1)]+(s if len(s)<2else[])
share|improve this answer
1  
I changed the heading to Python because it works in Python 3 also. – Leaky Nun 1 hour ago

Actually, 5 bytes

S;F@N

Try it online!

Explanation:

S;F@N
S      sort
 ;     dupe
  F    first element
   @N  and last element
share|improve this answer
    
I'm afraid you need to revert to the longer version because of singleton. – Leaky Nun 1 hour ago

Mathematica, 18 bytes

Sort[#][[{1,-1}]]&

Sorts the array and extracts the first and last values.

share|improve this answer

Octave, 20 bytes

@(n)sort(n)([1,end])

This sorts the input vector and outputs the first and last value.

share|improve this answer

Haskell, 36 bytes

(\l->(head l,last l)).Data.List.sort

Would love to use &&& here but imports are so costly...

share|improve this answer

Retina, 17 bytes

O#`
$
¶$`
Ss`¶.+¶

Input and output are linefeed-separated.

Try it online!

Explanation

O#`

Sort lines by numerical value.

$
¶$`

Duplicate the entire input to make sure that the first and last element are distinct.

Ss`¶.+¶

Remove all intermediate lines, leaving only the minimum and maximum.

share|improve this answer

R, 30 bytes

sort(l=scan())[c(1,length(l))]

Not that original, but hey !

share|improve this answer

V, 12 bytes

:sor
ò2Gjkd

Try it online!

Credit to DJMcMayhem for this.

share|improve this answer
    
\o/ Yay, I'm no longer the only person to have ever used this language! – DJMcMayhem 44 mins ago

Brain-Flak 220 218 bytes

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

Try It Online!

Explanation

First it doubles the top value (in cast the list is only one long)

(({}))

Then it uses my bubble sort algorithm:

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

Then it picks up the top value of the stack (i.e. the min)

({}<...>)

Then it pops until the height of the stack is one:

((())){{}{}([][()])}{}
share|improve this answer

Brachylog, 9 bytes

oOtT,Oh:T

Try it online!

share|improve this answer

Ruby, 38 23 bytes

def m(v) puts v.sort![0];$><<v[-1];end

->v{[v.sort![0],v[-1]]}

Thanks @Jordan!

share|improve this answer
1  
You don't need the space after def m(v). Regardless, you can save 15 bytes by using a lambda and just returning an array: ->v{[v.sort![0],v[-1]]}. – Jordan 1 hour ago

BASH 38

grep -o '[^ ]*'|sort -g|sed -n '1p;$p'
share|improve this answer

Processing, 59 bytes

void m(int[] x){int[] y=sort(x);print(y[0],y[y.length-1]);}

Processing doesn't actually let me read from stdin that I've been able to find, and I don't know if its internal Java compiler supports lambdas (and its been so long since I've had to write serious Java that I don't remember how).

share|improve this answer

05AB1E, 6 4 bytes

{Âø¬

Explanation

{     # sort list
 Â    # bifurcate
  ø   # zip
   ¬  # head

Try it online

share|improve this answer
    
fails for array with 1 element – Dmitry Kudriavtsev 1 hour ago
    
That won't work, you need to return it twice in that case – Dmitry Kudriavtsev 57 mins ago
    
@DmitryKudriavtsev OK. I suggest adding that to the challenge specification. – Emigna 55 mins ago

CJam, 10 9 bytes

q~$_(p;W>

Try it online.

I'm really not good at CJam.

q~          e# eval input
  $         e# sort
   _        e# duplicate
    (       e# pop first
     p      e# print
      ;     e# remove array
       W>   e# get last element
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.