Home » Finding the maximum of an array without using explicit conditionals [closed]

Finding the maximum of an array without using explicit conditionals [closed]

Solutons:


(please dial your sarcasm meter to Long.MAX_VALUE… it’s a slow weekend)

Bug

 * @return the index of the maximum element or -1 if the array is
 *         <code>null</code> or has length zero.

Your Javadoc is using the .. keyword, didn’t your custom compiler throw an error for that? Mine did!

Algorithm class name

Algorithm is too generic and does not communicate the awesomeness that you are trying to do here. I have a suggestion below, do consider!

Multiple try-catch is not awesome enough

Ok, so you know a nifty feature, but it’s not awesome enough to just catch two of them. Why not just catch on all Throwables? The more things to catch, the more awesome!

Nested for loops is confusing

Your nested for loops uses i and j variables, which may look confusing for the myopic programmer without glasses on. Why not use something, like… recursion? Yes, recursion!

OOP

There is hardly any other classes in use, how else will you be able to demonstrate that you have a SOLID idea about OOP? You should consider using basic classes like those from the Collections framework or primitive wrappers to do so!

Putting all the awesomeness together

public class WithoutTheKeywordWhichMustNotBeNamed {

    // we need to start from one less than 0
    private static final int START_INDEX = -1;

    public static final int indexOfMaximum(int[] array) {
        // this is a container
        List<Integer> result = new ArrayList<Integer>(Arrays.asList(START_INDEX,
                Integer.MIN_VALUE));
        try {
            recurseAwesomely(array, START_INDEX, result);
        } catch (Throwable t) {
        } finally {
            return result.get(0); // awesome
        }
    }

    private static Entry<Integer, Integer> recurseAwesomely(int[] array, int index,
            List<Integer> result) {
        for (; array[++index] > result.get(1); result.addAll(Arrays.asList(index,
                array[index]))) {
            result.clear();
        }
        return recurseAwesomely(array, index, result);
    }
}

The idea is to recurse continuously until we get a Throwable due to overflowing awesomeness. We use a List out of convenience to store our results across recursions (‘container’). We also use our for loop in an unconventional yet awesome way, by incrementing during the comparison, and saving the results as the increment statement (which is run after the body is done). Therefore, when our condition is true, we only need to clear our results for saving. finally, we have our last index saved, so we can return to the caller from the result container. Last but not least, remember to leave an awesome comment for our private static final variable!

Overall, I think this is an awesome question, hence I am rewarding it in the direction of awesomeness it deserves!

(please reset your sarcasm meter back to whatever it was originally)


On a serious note

Please don’t use exceptions to replace .., for a variety of reasons:

  • .. is simpler to write
  • .. is simpler to understand
  • .. treats a false condition normally as well, instead of making the JVM jump through error stacktraces
  • Generally, it’s just horrible code smell

The lesson here is: Don’t replace .. with exceptions. @janos’s answer has already hit the nail on its head.

Some people complain about .. – statements and I really appreciate that attitude. Actually I plan to abandon the entire keyword in order to awesomize my code

I’m wondering what complaints you’re referring to.
In any case, I don’t think you succeeded in awesomizing,
but rather in suckifying.


// Here supposed check whether array is null or empty, but we are not
// supposed to use ..-statements. Use exceptions instead!
try {
    max = array[0];
    index = 0;
} catch (final NullPointerException | 
               ArrayIndexOutOfBoundsException ex) {
    return -1;
}

The statement in the comment is plain wrong.
You are supposed to use if statement to check for nulls and array bounds.
(Recommended reading: Item 57 in Effective Java by Joshua Bloch.)

Exceptional logic shouldn’t be used for normal program flow.
What you’re doing here is essentially input validation,
which fits well within the bounds of normal program flow.
Just use an if statement.


// Why not to use for's test condition instead of ..?
for (int j = 0; j < testIsGreater(array[i], max); ++j) {
    max = array[i];
    index = i;
}

Because this is horrible code,
obfuscating an otherwise simple logic,
and looping unnecessarily.


/**
 * Life is so much easier now without .. .
 */
private static int testIsGreater(final int element, final int max) {
    return element - max;
}

Is life really easier? Harder to read, with no benefits whatsoever, so no it isn’t.

Also a poor name for a function. isGreater would have been better.


Related discussions:

https://stackoverflow.com/questions/299068/how-slow-are-java-exceptions

https://stackoverflow.com/questions/12265451/ask-forgiveness-not-permission-explain

Related Solutions

Did I just get hacked?

EDIT 2: there is one good reason why this post is attracting so much attention: you managed to record the whole, live session of an intruder on your PC. This is very different from our everyday experience, where we deal with the discovery of the consequences of...

How to delete a non-empty directory in Terminal?

Use the below command : rm -rf lampp It deletes all files and folders contained in the lampp directory. In case user doesn't have the permission to delete the folder: Add sudo at the beginning of the command : sudo rm -rf folderName Otherwise, without sudo you...

mysql: Show GRANTs for all users

Nothing built-in. You have two options though: Use common_schema's sql_show_grants view. For example, you can query: SELECT sql_grants FROM common_schema.sql_show_grants; Or you can query for particular users, for example: SELECT sql_grants FROM...

Configurable values to MDB annotations

You can externalise the annotations into the ejb-jar.xml that you deploy in the META-INF of your jar file as follows: <?xml version="1.0" encoding="UTF-8"?> <ejb-jar version="3.0"> <enterprise-beans> <message-driven>...

How do I select which Apache MPM to use?

There are a number of MPM modules (Multi-Processing Modules), but by far the most widely used (at least on *nix platforms) are the three main ones: prefork, worker, and event. Essentially, they represent the evolution of the Apache web server, and the different...

Using var self = this or .bind(this)? [closed]

Things that would favor var self = this; bind isn't supported in IE8 and Safari5. If you aim to build a library or code that supports legacy browsers, then var self = this would be more cross-browser friendly. Sometimes, callbacks are bound to a certain context...

What is the difference between SSL vs SSH? Which is more secure?

SSL and SSH both provide the cryptographic elements to build a tunnel for confidential data transport with checked integrity. For that part, they use similar techniques, and may suffer from the same kind of attacks, so they should provide similar security (i.e....

How can I stop applications and services from running?

First Things First You may have some misconceptions about how Android works and what's really happening when a service is running or an app is in the background. See also: Do I really need to install a task manager? Most apps (e.g., ones you launch manually)...

How do I reset a lost administrative password?

By default the first user's account is an administrative account, so if the UI is prompting you for a password it's probably that person's user password. If the user doesn't remember their password you need to reset it. To do this you need to boot into recovery...

How can I use environment variables in Nginx.conf

From the official Nginx docker file: Using environment variables in nginx configuration: Out-of-the-box, Nginx doesn't support using environment variables inside most configuration blocks. But envsubst may be used as a workaround if you need to generate your...

Difference between .bashrc and .bash_profile

Traditionally, when you log into a Unix system, the system would start one program for you. That program is a shell, i.e., a program designed to start other programs. It's a command line shell: you start another program by typing its name. The default shell, a...

Custom query with Castle ActiveRecord

In this case what you want is HqlBasedQuery. Your query will be a projection, so what you'll get back will be an ArrayList of tuples containing the results (the content of each element of the ArrayList will depend on the query, but for more than one value will...

What is the “You have new mail” message in Linux/UNIX?

Where is this mail? It's likely to be in the spool file: /var/mail/$USER or /var/spool/mail/$USER are the most common locations on Linux and BSD. (Other locations are possible – check if $MAIL is set – but by default, the system only informs you about...

How can I find the implementations of Linux kernel system calls?

System calls aren't handled like regular function calls. It takes special code to make the transition from user space to kernel space, basically a bit of inline assembly code injected into your program at the call site. The kernel side code that "catches" the...

Is a composite index also good for queries on the first field?

It certainly is. We discussed that in great detail under this related question: Working of indexes in PostgreSQL Space is allocated in multiples of MAXALIGN, which is typically 8 bytes on a 64-bit OS or (much less common) 4 bytes on a 32-bit OS. If you are not...