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 1062177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:35:02+00:00 2026-05-16T18:35:02+00:00

What would be the best way to optimize this code below to make it

  • 0

What would be the best way to optimize this code below to make it more efficient while applying the ‘best practices’. This is just a simple school project and it works, I’ve tested it. But I just feel like there’s a better and more efficient way to write this method. What do you think?

  1. I have an array that
    is pre-populated with a bunch of
    numbers. The getMax() method
    retrieves the highest number in the
    array. But if the array is empty it
    returns -1.
  2. nElems is simply a variable that keep tracks of how many elements are present in the array.

  3. array is the private array declared at the beginning of the class.

    public int getMax() {
    int k = 0;
    int max = array[0];
    
    for(int j=0; j<nElems; j++) {
        if(max < array[j])
            max = array[j];
    }
    
    if(k == nElems)
        return -1;
    else return max;
    } // end of method
    

Oh and what should I name my k variable to make it more readable? Its purpose is to be 0, so it could be checked against the number of elements in an array to return either -1 or highest number;

Assume all array values are positive.

  • 1 1 Answer
  • 1 View
  • 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-05-16T18:35:03+00:00Added an answer on May 16, 2026 at 6:35 pm

    Its purpose is to be 0, so it could be
    checked against the number of elements
    in an array to return either -1 or
    highest number;

    Zero doesn’t need a name – if you mean 0, use the literal 0.

    Your loop is fine. There are things you could do to try to optimise it, but they won’t necessarily help. Almost always you may as well just leave such localised, “keyhole” optimisation to the JIT. If you just want to try a few things as a learning exercise, then you could experiment as follows:

    • rather than using the array twice, set a variable to the array value, then use it twice.
    • unroll the loop by different amounts. Be careful not to make the error of accessing the wrong indices if numElem isn’t a multiple of the number of times you’re unrolling.
    • work backwards through the array instead of forwards
    • return immediately if you encounter a value of Integer.MAX_VALUE. You aren’t going to find anything bigger. It’s difficult to define the performance effect of this, since it will speed it up for large arrays with a MAX_VALUE that isn’t too near the end, but most likely slow it down for everything else. It might be interesting to see how much difference it makes, though.
    • anything else you can think of.

    I’m not saying any of these will make a difference, faster or slower, but they could. So run them and time how fast each goes. What you’ll most likely learn, is that compared with leaving it to the JIT this stuff isn’t worth your time. But you never know 😉

    The surrounding code can be cleaned up, though, to make it more comprehensible rather than to make it faster. For example:

    public int getMax() {
        int max = -1;
        if (nElems > 0) {
            max = array[0];
            for(int j=1; j<nElems; j++) {
                if(max < array[j])
                    max = array[j];
            }
        }
        return max;
    }
    

    This also removes the error in your code, that if the array is size 0, then you access out of bounds in the second line. ColinD’s way of doing that is just as good, I’m showing something different for the sake of variety.

    Here’s the shorter code assuming all values are non-negative:

    public int getMax() {
        int max = -1;
        for(int j=0; j<nElems; j++) {
            if(max < array[j])
                max = array[j];
        }
        return max;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make a code snippet database web application. Would the best way
What would be the best way to something like this where a timestamp is
What would be the best way and more idiomatic to break a string into
I need to know if there's a best way to optimize this kind of
postgres 9.2 supports json columns. what would be best way to extend postgres to
What would be the best way to display a dialog whenever my app receives
What would be the best way to version a rails application? We want to
What would be the best way to properly concatenate string with an url inside?
What would be the best way to convert a 50-digit String to a BigInteger
What would be the best way in Python to parse out chunks of text

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.