Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8474657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:41:18+00:00 2026-06-10T17:41:18+00:00

edit: On odd change that someone is reading this, I’d like to add one

  • 0

edit:
On odd change that someone is reading this, I’d like to add one last thing.
Assuming that the three values in question are already in memory and they are not changed, I’ve counted no less than 14 instructions to be able to achieve this feat.

I’s very much like this confirmed if anyone can.

[top edit end]

The problem is simple. I have three integer values and I need to find the largest and the smallest. By largest I mean not smallest or in between and vice versa.

As I was unable to find a “quality solution” online, I had to make my own attempt.

if(a > b) {
  if(a > c) {
    high = a;
    if(b > c) {
      low = c;
    }
    else {
      low = b;
    }
  }
  else {
    if(b > c) {
      high = b;
      low = c;
    }
    else {
      high = c;
      low = b;
    }
  }
}
else if(a > c) {
  if(b > c) {
    high = b;
    low = c;
  }
  else {
    high = c;
    low = b;
  }
}
else {
  low = a;
  if(b > c) {
    high = b;
  }
  else {
    high = c;
  }
}

Assuming I have not made any mistakes, this should solve the problem using three conditionals.

Assuming it works as intended, I’m rather pleased with my effort actually, however my intention was to find the most efficient algorithm, thus I ask you now what that is.

Best regards.

Edit:
I’ve reviewed the solutions proposed so far and they’re all nice.

My favorite so far.

if(a>b) {
  max = a;
  min = b;
}
else {
  max = b;
  min = a;
}
if(c>max)
  max = c
else if(c< min)
  min = c

2-3 sigments and 2-3 conditionals, if I’m not mistaken. That’s impressive.

small revision of the above, using ‘a’ as alias for ‘min’.

if(a>b) {
  max = a;
  a = b;
}
else {
  max = b;
}
if(c>max)
  max = c
else if(c< a)
  a = c

If only there was an easy way to exchange variables… Well, the only thing I can think of is that could eliminate the need for ‘max’, at least in C and C derivatives, certainly wouldn’t be efficient, other than in terms of memory use, and I can affort to spend an extra 4 bytes. 😉

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:41:20+00:00Added an answer on June 10, 2026 at 5:41 pm
    def min_max(a, b, c):
        if a > b:
            min, max = b, a   # two assignments
        else:
            min, max = a, b   # ditto
    
        if c > max:
            max = c
        elif c < min:
            min = c
    
        return (min, max)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

// Edit: oh wow. It's odd that I've been working on this for a
EDIT : It turned out that this can only be done through an external
Edit: Rolled back Wills change as it was wrong I'm experiencing an odd problem
This is a weird one that I haven't been able to find a good
I have an odd issue that's driving me nuts by now. Hopefully someone here
This seems to be a very odd problem that I cannot figure out for
This is an odd one. Note this is cut down example code, and misses
EDIT 07/14 As Bill Burgess mentionned in a comment of his answer, this question
EDIT: I was an idiot. I simply had an image that was vertically long,
EDIT: See my answer below--> I am wanting to have a view that when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.