• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Finding the maximum of an array without using a loop

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.. Is thr any way of finding the maximum and minimum of an array without using any loops, or if conditions??

Tnx..
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could sort the array and get the first/last value, though the Arrays.sort() stuff will ultimately be using loops and conditions.
[ October 06, 2006: Message edited by: Paul Sturrock ]
 
Wanderer
Posts: 18671
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is pretty much impossible without at least one loop and at least one if condition (maybe two) inside the loop. Or maybe a ternatry operator instead of an if, but the difference seems immaterial. You could hide the loop and conditions by putting them in a separate method - or by calling a pre-existing method such as Arrays.sort() as Paul suggests. But the loop will have to exist somewhere, regardless.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
think about it in non-programming terms.

How would YOU, by hand, find the largest value in a list of numbers? you'd have to look at each and every number.

if you don't know how many numbers there are, you have to keep looking until you run out.

if you DO know the exact number, you could hard-code your java to say

biggest = element 0;
if element 1 > biggest, biggest = element 1;
if element 2 > biggest, biggest = element 2;
...
if element x > biggest, biggest = element x;

this is not practical if the array has hundreds of elements, or you want the size to dynamically change, but it does allow you to avoid a loop.
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One way could be inserting the items in order into the array, this way you simply get the first and the last item of the array when you want to find the maximum and the minimum.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If this is a trick question or a geek challenge to not use a loop or an if test, I bet you could.

Do you know another way to work over a set of things besides a loop? Like, what would you use for a tree? Is there a way other than "if a>b" to get the larger or smaller of two values?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.*;
import java.util.Arrays;  

public class Main
{
public static void main(String[] args) {
System.out.println("Hello World");
int [] arr = {5,5,6,2,7,9,};
Arrays.sort(arr);

System.out.println("max element="+ arr[arr.length-1]);
System.out.println("min element=" + arr[0]);
   }
}
I think by using sorting we can achieve..
 
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it is going to depend on the definition of loop and where it is placed:

you could do a recursive walk of the array and return back through using just a comparison of some type, but that is horrible use of resources, but then if this is a homework problem or a test of some sort, there are almost always ways of doing things--no matter how horrible it my be.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would be helpful if the OP had said what sort of elements the array has.

Les Morgan wrote:it is going to depend on the definition of loop . . ..

In which case, does the following count?I don't like sorting the array as a solution. I think the original question was more a party piece than proper programming
 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is going to have to, once again, depend on the definition of loop.  That camparator will have some looping involved.
 
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only way to avoid a loop is to use recursion. Recursion, is by definition, not a iteration and therefore not a loop:

 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Antonio Moretti wrote:. . . Recursion, is by definition, not a iteration and therefore not a loop . . .

You can prove that iteration and recursion are semantically equivalent. As I said earlier, this is a party trick and not programming. Math#max uses ?: behind the scenes, which is equivalent to an if‑else.
 
Antonio Moretti
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Antonio Moretti wrote:. . . Recursion, is by definition, not a iteration and therefore not a loop . . .

You can prove that iteration and recursion are semantically equivalent. As I said earlier, this is a party trick and not programming. Math#max uses ?: behind the scenes, which is equivalent to an if‑else.



yes of course. But I wondered why he ask the question and my thought was that questioner is wanting an alternative approach to using loops, which would be recursion, since the two main way of implementing sorting is loops and recursion.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Antonio Moretti wrote:. . . I wondered why he ask the question . . .

Because somebody else asked him how he would implement that.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Keshini Weerasuriya wrote:Is thr any way of finding the maximum and minimum of an array without using any loops, or if conditions??


My answer would be "who cares?"  If there is, I wouldn't want to ever see it in production code, so to me it doesn't matter if it exists or not.
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
 
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And already answered pretty well, 14 years ago.  Though I will admit that using recursion instead of looping is a new point - it's arguable, but worth mentioning.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic