5

I have two arrays, one is very large (more than million entries) and other array is small (less than 1000 entries), what would be the best approach to find maximum number out of all entries in arrays ?

Thanks.

Isaac Waller
  • 32,709
  • 29
  • 96
  • 107
Rachel
  • 100,387
  • 116
  • 269
  • 365
  • Iterate over each array, keeping track of the maximum so far. Is there a trick to this question? You have to at least look at every value to find the maximum. – hughdbrown Aug 29 '09 at 03:24
  • 1
    I must say, it is remarkable how many variations of the user-name "rachel" have been appearing lately. :) On a more serious note, here is a very-much related question: http://stackoverflow.com/questions/1042507/finding-smallest-value-in-an-array-most-efficiently – agorenst Aug 29 '09 at 03:25
  • @Agor - Three users named 'Rachel' have signed up today... odd. – Charlie Salts Aug 29 '09 at 03:38
  • Ah, it's homework. I didn't realize that at first. – hughdbrown Aug 29 '09 at 03:51
  • No evidence that this is homework - I'm untagging it. – Isaac Waller Aug 30 '09 at 05:29

8 Answers8

16

If the arrays are unsorted then you must do a linear search to find the largest value in each. If the arrays are sorted then simply take the first or last element from each array (depending on the sort order).

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
5

If you think about it, if you want to find the highest value, you have to check all the values. There's no way around that (unless the arrays are sorted, which is easy - just take the last value (or first if sorted in descending order) of each array and take the biggest). Example:

int highest = array1[0]; // note: don't do this if the array could be empty
for(int i = 1; i < array1.length; i++) {
    if(highest<array1[i]) highest = array1[i];
}
for(int i = 0; i < array2.length; i++) {
    if(highest<array2[i]) highest = array2[i];
}  
// highest is now the highest
rlc
  • 2,808
  • 18
  • 23
Isaac Waller
  • 32,709
  • 29
  • 96
  • 107
4

If your arrays are already sorted, you can just jump to the end with the maximum.

If your arrays are not sorted, you'll have to run over the entire list, tracking the largest value seen so far.

Mark Rushakoff
  • 249,864
  • 45
  • 407
  • 398
1

Here I am giving the simple code for finding the Maximum value from an int Array. My Logic is :- array is int[] arr={8,5,6,7,3,4,9} . first take a temporary variable and put the first value in that variable and assuming that this is the maximum value i.e, tempValue=arr[0]. And inside the for loop take an if block and checking second value is greater than first or not. Similarly if block will check automatically for the rest values. Finally maximum value will assigning in the temporary variable and got the result Maximum value is 9 in the given array.

public class MaxIntArray{

      public static void main(String[] args){
        
        
        int[] arr={8,5,6,7,3,4,9};
        
        int tempValue=arr[0];

        for(int i=0;i<arr.length;i++){
            if(arr[i]>tempValue){
                tempValue=arr[i];
            }
        
        }
        System.out.println("\n Maximum Value in the Given Array = "+tempValue);
    
     }

}

Output is:

Maximum Value in the Given Array = 9

  • The question cares about a case with "(more than million entries)", how does it take into account this aspect? – emecas Dec 25 '14 at 17:13
  • @emecas, arr.length will check for all entries in the array. I have given the logic for the unsorted array. i.e, need to check every element one by one like linear search. – Fazal JavaBigner Dec 26 '14 at 16:02
0

For unsorted array, you can initialize a maximum number variable with value 0 (given the array is made of positive values) then iterate over all the items in the array assigning the larger number at each iteration to the maximum variable.

    int[] values = {8,3,7,10,5};
    max = 0; 
    for(int i = 0;i < values.length;i++){
     if(values[i] > max){
        max = values[i];
       }
    }
    System.out.println(max);
0

Reduce to find highest number:

This example uses Swift.

let max: UInt8 = [1,4,3].compactMap { $0 }.reduce(UInt8(0)) { $0 > $1 ? $0 : $1 } // 4
Sentry.co
  • 5,355
  • 43
  • 38
0

This solution is when you have an Array of objects, and you wish to find the max of an attribute from it.

Here, we have a Array of jobs (with their respective profits and deadlines) and we are required to find the most profitable job.

class Job
{
    int deadline; int profit;
    
    public Job(int deadline, int profit)
    {
        this.profit = profit;
        this.deadline = deadline;
    }
    
    
    public int getDeadline()
    {
        return deadline;
    }
    
    public int getProfit()
    {
        return profit;
    }
    
    public String toString()
    {
        return "Job [deadline:"+this.deadline+" profit:"+this.profit+"]";
    }
}

Method to find the most profitable job.

static int jobSequenceWithMaxProfit(Job[] jobsAndProfits)
    {
        List<Job> lst= Arrays.asList(jobsAndProfits);
        int maxProfit= Collections.max(lst, (Job a1, Job a2)->{ return a1.getProfit()- a2.getProfit();}).getProfit();
        
        return maxProfit;
    }
-1

We can reduce your number of operations or comparison to 3(n/2-2). from 2n(n for finding maximum number using linear search and n for minimum). Let say we have an array of elements [1,9,8,7,4,5,1,4,7,8,1,6]. Set the first element to Max=1 and the next to Min=9, now take simultaneously next two elements of the array compare them and then compare with Max and Min. So one iteration require only 3 comparison but the array is reduce to n/2. Thus the total number of comparison will be 3(n/2-2). Example:

Max=arr[1];

Min=arr[2];

for(int i=3; i< arr.length;i=i+2)

{

if(arr[i]>arr[i+1]) 

{

if(Max < arr[i])

Max=arr[i];

if(Min > arr[i+1])

Min=arr[i+1];

}

else 

{

if(Max < arr[i+1])

Max=arr[i+1];

if(Min > arr[i])

Min=arr[i];

}

}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • 2
    How, actually, for an unsorted array of 10 elements n-1 (10-1=9) comparisons need to be made. In your case 3(n/2-2) i.e. 3(10/2-2)=9 comparisons need to be made, both are equal, for 20 element array, your method needs 24 comparisons, how is it superior? – Ironluca Dec 12 '14 at 10:33
  • This code seems incorrect. If the first element of the array is the minimum, this code won't find it. If the second element of the array is the maximum, this code won't find that either. – Eric Backus Jul 01 '20 at 16:10