Rachel
Rachel

Reputation: 103397

Find Maximum Integer in Array?

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.

Upvotes: 5

Views: 40523

Answers (8)

djklicks-dhananjay
djklicks-dhananjay

Reputation: 29

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;
    }

Upvotes: 0

Isaac Waller
Isaac Waller

Reputation: 32730

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

Upvotes: 5

Fazal JavaBigner
Fazal JavaBigner

Reputation: 11

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

Upvotes: 1

Sentry.co
Sentry.co

Reputation: 5569

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

Upvotes: 0

Yeku Wilfred Chetat
Yeku Wilfred Chetat

Reputation: 89

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);

Upvotes: 0

Muhammad.
Muhammad.

Reputation: 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];

}

}

Upvotes: -1

Mark Rushakoff
Mark Rushakoff

Reputation: 258128

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.

Upvotes: 4

Andrew Hare
Andrew Hare

Reputation: 351466

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).

Upvotes: 17

Related Questions