Hi All,
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.
Hi All,
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.
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).
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.
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 (or first if sorted descendingly) of each array and take the biggest). Example:
int highest = array1[i]; // note: don't do this if the array could be empty
for(int i = 0; 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