finding two max values in Java - java

Can anyone help me please? I want to find the maximum and the 2nd maximum value of an Array using a method. I already know how I can get the Minimum and the 2nd minimum value, it's basically almost the same, but the output is still wrong ?
This is the method how I find the two minimum values.
public static void minimum( int [] a ) {
int min;
for (int i = 0; i< a.length; i++) {
for (int j = min = i; j< a.length; j++) {
if (a[j] < a[min]) min = j;
if(min>i) {
int temp = a [i];
a [i] = a [min];
a [min] = temp;
}
}
so , to find the maximum I thought it's just to change < to > from this line :
if (a[j] < a[min]) min = j;`
so, instead of swapping smaller values, bigger values will be swapped. But it's wrong and I don't get it.
Thank you !

If you want to code by yourself,the code may help you:
public static void maxNum(int[] a) {
int length = a.length;
if (length == 0) {
System.out.println("the length of input array 0");
return;
}
if (length == 1) {
System.out.println("the max is " + a[0]);
return;
}
int max = a[0] > a[1] ? a[0] : a[1];
int secondMax = a[0] > a[1] ? a[1] : a[0];
for (int i = 2; i < length; i++) {
int value = a[i];
if (value > max) {
int tmp = max;
max = value;
secondMax = tmp;
} else if (value > secondMax) {
secondMax = value;
}
}
System.out.println("the max is " + max);
System.out.println("the second max is " + secondMax);
}
If you don't want to code yourself,you can use tools in java like Arrays.sort() to sort the array first and get what you want.

Related

What is going on in my Java code to make the answers negatives?

public static int distanceBetweenMinAndMax (double[] list) {
if (list == null || list.length == 0) return 0;
int min = 0;
int max = 0;
for (int i = 1; i < list.length; i++)
{
if (list[min] > list[i])
{
min = i;
}
if (list[max] < list[i])
{
max = i;
}
}
int distanceBetweenMinandMax = max - min;
return distanceBetweenMinandMax;
}
Okay, the point of the exercise is to find the distance from the max index to the min index. No matter what, my test cases are messing up. I'll be expecting 1 and get -1. Expect 3 and get -3. And so on. I assumed it had to be the int distance... = max - min line. But even if I flip them, the result is still negative.
I've tried everything for hours. Does anyone have any input on what may be wrong here that causes the answer to be negative?
NOTE: you just can't change the public static inc to a double.
use Math.abs(min-max) -- because the index positions of Max and Min values can be crossed based on the input
int distanceBetweenMinandMax = Math.abs(min - max);
Try changing from:
int distanceBetweenMinandMax = max - min;
To:
int distanceBetweenMinandMax = max>min?(max-min):(min-max);
try this solution
public static int distanceBetweenMinAndMax (double[] list) {
if (list == null || list.length == 0) return 0;
double min = 0;
double max = 0;
int mxi=0;
int mni=0;
for (int i = 0; i < list.length; i++)
{
if (min > list[i])
{
min = list[i];
mni=i;
}
if (max < list[i])
{
max = list[i];
mxi=i;
}
}
int distanceBetweenMinandMax = mxi - mni;
return distanceBetweenMinandMax;
}
so if the result is (-) then max value is before the min value index
and if the result is (+) then max value is after the min value index
#Ptoh64 Your code is almost correct actually you get negative distance only when the max value comes earlier than the min value in the array. I mean the index of max value is less than the index of min value in array.
You have to return the absolute difference of max and min to avoid negative value for distance e.i
int distanceBetweenMinandMax = Math.abs(max - min)
The whole code will be same except above changes
public static int distanceBetweenMinAndMax(double[] list) {
if (list == null || list.length == 0)
return 0;
int min = 0;
int max = 0;
for (int i = 1; i < list.length; i++) {
if (list[min] > list[i]) {
min = i;
}
if (list[max] < list[i]) {
max = i;
}
}
int distanceBetweenMinandMax = Math.abs(max - min); // here take absolute difference
return distanceBetweenMinandMax;
}

Retrieving the index of a maximum value of an array

Say I have an array with elements {1,5,2,3,4}.
I have the code to find the maximum value, which is 5.
I would like to remove this value from the array by replacing array[1] with array[2], array[2] with array[3], etc., and then making a new array with one less index (to not repeat the last value).
How can I find/state the index of the maximum value for an array, knowing what the maximum value is?
Would it be a lot different if we had an array where the maximum value occurs twice? {1,5,5,2,3}
Thank you very much.
EDIT1: I have figured out how to do it for one instance incorporating int maxIndex = 0; and setting it at the same time as the max value is set.
Now I just need to figure out for multiple instances.
int[] score = new int[5];
for (int i=0 ; i<=4 ;i++)
{
System.out.println("enter Score");
score[i] = keyb.nextInt();
}
System.out.println(Arrays.toString(score)); //need import java.util.Arrays;
int max = score[0];
int maxIndex = 0;
for (int i = 1 ; i<=score.length-1 ; i++)
{
if (score[i] > max)
{max = score[i];
maxIndex= i; }
}
System.out.println("The maximum is " +max); //this finds the maximum. Now say we want to remove the maximum (no matter what the position)..
System.out.println("it is located at index " + maxIndex);
For multiple instances, you can use a Set to keep track of the indices in the array that corresponds to the max number.
int max = score[0];
Set<Integer> maxIndices = new HashSet<>(); // a set that contains the indices of the max number in the array
maxIndices.add(0);
for (int i = 1; i <= score.length - 1; i++) {
if (score[i] > max) {
max = score[i];
maxIndices.clear(); // clear the set as we have a new max number
maxIndices.add(i);
} else if (score[i] == max) {
maxIndices.add(i); // keep track of all the indices in the array that corresponds to the max number
}
}
// create the new array with the new size
int newArrayWithoutMaxNums[] = new int[score.length - maxIndices.size()];
int newCounter = 0;
for (int i = 0; i < score.length; ++i) {
if (!maxIndices.contains(i)) { // determine if the score is the max
newArrayWithoutMaxNums[newCounter++] = score[i];
}
}
for (int i = 0; i < newArrayWithoutMaxNums.length; ++i) {
System.out.print(newArrayWithoutMaxNums[i] + "\t");
}
You can try something like this:
public class findMaxIndex {
public static void main(String[] args) {
int[] score = new int[]{1,5,5,5,5,2,4,6,6,6,6,1,4,1};
int max = score[0];
int[] maxIndexArray = new int[score.length];
int j = 0;
for (int i = 1; i <= score.length-1 ; i++) {
if (score[i] > max) {
max = score[i];
j = 0;
maxIndexArray = new int[score.length];
maxIndexArray[j++] = i;
}
else if (score[i] == max) {
maxIndexArray[j++] = i;
}
}
System.out.println("The maximum is " +max); //this finds the maximum. Now say we want to remove the maximum (no matter what the position)..
System.out.println("it is located at index ");
for (int i = 0; i < maxIndexArray.length - 1; i++) {
System.out.println(maxIndexArray[i]);
}
}
}
There are better things that you can do. However, I figure at least one simple example is good. Thus the below, though I certainly recommend trying to figure out the other answers as well even if the methods used are things you haven't learned yet.
Let's start by altering your code slightly.
int max = score[0];
int maxIndex[] = new int[score.length]
int maxCount = 0;
for (int i = 1 ; i < score.length ; i++){
if (score[i] > max){
max = score[i];
maxCount = 0;
maxIndex[maxCount++] = i;
}
else if (score[i] == max)
{
//maxCount++ performs maxCount = maxCount+1; after the specified operation
maxIndex[maxCount++] = i;
}
}
System.out.println("The maximum is " + max);
System.out.print("This value occurs at: ");
for(int i = 0; i < maxCount; i++){
System.out.print(maxIndex[i]);
}

Find smallest integer value in array list in Java without Arrays.sort

How can I find the smallest value in a int array without changing the array order?
code snippet:
int[] tenIntArray = new int [10];
int i, userIn;
Scanner KyBdIn = new Scanner(System.in);
System.out.println("Please enter 10 integer numbers ");
for(i = 0; i < tenIntArray.length; i++){
System.out.println("Please enter integer " + i);
userIn = KyBdIn.nextInt();
tenIntArray[i] = userIn;
}
I am not sure how I can find the smallest array value in the tenIntArray and display the position
For example the array holds - [50, 8, 2, 3, 1, 9, 8, 7 ,54, 10]
The output should say "The smallest value is 1 at position 5 in array"
This figure should be helpful :
Then to answer your question, what would you do on paper ?
Create and initialize the min value at tenIntArray[0]
Create a variable to hold the index of the min value in the array and initialize it to 0 (because we said in 1. to initialize the min at tenIntArray[0])
Loop through the elements of your array
If you find an element inferior than the current min, update the minimum value with this element and update the index with the corresponding index of this element
You're done
Writing the algorithm should be straightforward now.
Try this:
//Let arr be your array of integers
if (arr.length == 0)
return;
int small = arr[0];
int index = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] < small) {
small = arr[i];
index = i;
}
}
Using Java 8 Streams you can create a Binary operator which compares two integers and returns smallest among them.
Let arr is your array
int[] arr = new int[]{54,234,1,45,14,54};
int small = Arrays.stream(arr).reduce((x, y) -> x < y ? x : y).getAsInt();
The method I am proposing will find both min and max.
public static void main(String[] args) {
findMinMax(new int[] {10,40,50,20,69,37});
}
public static void findMinMax(int[] array) {
if (array == null || array.length < 1)
return;
int min = array[0];
int max = array[0];
for (int i = 1; i <= array.length - 1; i++) {
if (max < array[i]) {
max = array[i];
}
if (min > array[i]) {
min = array[i];
}
}
System.out.println("min: " + min + "\nmax: " + max);
}
Obviously this is not going to one of the most optimized solution but it will work for you. It uses simple comparison to track min and max values. Output is:
min: 10
max: 69
int[] input = {12,9,33,14,5,4};
int max = 0;
int index = 0;
int indexOne = 0;
int min = input[0];
for(int i = 0;i<input.length;i++)
{
if(max<input[i])
{
max = input[i];
indexOne = i;
}
if(min>input[i])
{
min = input[i];
index = i;
}
}
System.out.println(max);
System.out.println(indexOne);
System.out.println(min);
System.out.println(index);
Here is the function
public int getIndexOfMin(ArrayList<Integer> arr){
int minVal = arr.get(0); // take first as minVal
int indexOfMin = -1; //returns -1 if all elements are equal
for (int i = 0; i < arr.size(); i++) {
//if current is less then minVal
if(arr.get(i) < minVal ){
minVal = arr.get(i); // put it in minVal
indexOfMin = i; // put index of current min
}
}
return indexOfMin;
}
the first index of a array is zero. not one.
for(i = 0; i < tenIntArray.length; i++)
so correct this.
the code that you asked is :
int small = Integer.MAX_VALUE;
int i = 0;
int index = 0;
for(int j : tenIntArray){
if(j < small){
small = j;
i++;
index = i;
}
}
System.out.print("The smallest value is"+small+"at position"+ index +"in array");

How can i find the positions to the three lowest integers in an array?

I how can I find the positions of the three lowest integers in an array?
I've tried to reverse it, but when I add a third number, it all goes to hell :p
Does anybody manage to pull this one off and help me? :)
EDIT: It would be nice to do it without changing or sorting the original array a.
public static int[] lowerThree(int[] a) {
int n = a.length;
if (n < 2) throw
new java.util.NoSuchElementException("a.length(" + n + ") < 2!");
int m = 0; // position for biggest
int nm = 1; // position for second biggest
if (a[1] > a[0]) { m = 1; nm = 0; }
int biggest = a[m]; // biggest value
int secondbiggest = a[nm]; // second biggest
for (int i = 2; i < n; i++) {
if (a[i] > secondbiggest) {
if (a[i] > biggest) {
nm = m;
secondbiggest = biggest;
m = i;
biggest = a[m];
}
else {
nm = i;
secondbiggest = a[nm];
}
}
} // for
return new int[] {m,nm};
}
EDIT: I've tried something here but it still doesn't work. I get wrong output + duplicates...
public static int[] lowerthree(int[] a) {
int n= a.length;
if(n < 3)
throw new IllegalArgumentException("wrong");
int m = 0;
int nm = 1;
int nnm= 2;
int smallest = a[m]; //
int secondsmallest = a[nm]; /
int thirdsmallest= a[nnm];
for(int i= 0; i< lengde; i++) {
if(a[i]< smallest) {
if(smalles< secondsmallest) {
if(secondsmallest< thirdsmallest) {
nnm= nm;
thirdsmallest= secondsmallest;
}
nm= m;
secondsmallest= smallest;
}
m= i;
smallest= a[m];
}
else if(a[i] < secondsmallest) {
if(secondsmallest< thirdsmallest) {
nnm= nm;
thirdsmallest= secondsmallest;
}
nm= i;
secondsmallest= a[nm];
}
else if(a[i]< thirdsmallest) {
nnm= i;
thirdsmallest= a[nnm];
}
}
return new int[] {m, nm, nnm};
}
Getting the top or bottom k is usually done with a partial sort. There are versions that change the original array and those that dont.
If you only want the bottom (exactly) 3 and want to get their positions, not the values, your solution might be the best fit. This is how I would change it to support the bottom three. (I have not tried to compile and run, there may be little mistakes but the genereal idea should fit)
public static int[] lowerThree(int[] a) {
if (a.length < 3) throw
new java.util.NoSuchElementException("...");
int indexSmallest = 0;
int index2ndSmallest = 0;
int index3rdSmallest = 0;
int smallest = Integer.MAX_VALUE;
int sndSmallest = Integer.MAX_VALUE;
int trdSmallest = Integer.MAX_VALUE;
for (size_t i = 0; i < a.length; ++i) {
if (a[i] < trdSmallest) {
if (a[i] < sndSmallest) {
if (a[i] < smallest) {
trdSmallest = sndSmallest;
index3rdSmallest = index2ndSmallest;
sndSmallest = smallest;
index2ndSmallest = indexSmallest;
smallest = a[i];
indexSmallest = i;
continue;
}
trdSmallest = sndSmallest;
index3rdSmallest = index2ndSmallest;
sndSmallest = a[i];
index2ndSmallest = i;
continue;
}
trdSmallest = a[i];
index3rdSmallest = i;
}
}
return new int[] {indexSmallest, index2ndSmallest, index3rdSmallest};
}
This will have the three lowest numbers, need to add some test cases..but here is the idea
int[] arr = new int[3];
arr[0] = list.get(0);
if(list.get(1) <= arr[0]){
int temp = arr[0];
arr[0] = list.get(1);
arr[1] = temp;
}
else{
arr[1] = list.get(1);
}
if(list.get(2) < arr[1]){
if(list.get(2) < arr[0]){
arr[2] = arr[1];
arr[1] = arr[0];
arr[0] = list.get(2);
}
else{
arr[2] = arr[1];
arr[1] = list.get(2);
}
}else{
arr[2] = list.get(2);
}
for(int integer = 3 ; integer < list.size() ; integer++){
if(list.get(integer) < arr[0]){
int temp = arr[0];
arr[0] = list.get(integer);
arr[2] = arr[1];
arr[1] = temp;
}
else if(list.get(integer) < arr[1]){
int temp = arr[1];
arr[1] = list.get(integer);
arr[2] = temp;
}
else if(list.get(integer) <= arr[2]){
arr[2] = list.get(integer);
}
}
I'd store the lowest elements in a LinkedList, so it is not fixed on the lowest 3 elements. What do you think?
public static int[] lowest(int[] arr, int n) {
LinkedList<Integer> res = new LinkedList();
for(int i = 0; i < arr.length; i++) {
boolean added = false;
//iterate over all elements in the which are of interest (n first)
for(int j = 0; !added && j < n && j < res.size(); j++) {
if(arr[i] < res.get(j)) {
res.add(j, i); //the element is less than the element currently considered
//one of the lowest n, so insert it
added = true; //help me get out of the loop
}
}
//Still room in the list, so let's append it
if(!added && res.size() < n) {
res.add(i);
}
}
//copy first n indices to result array
int[] r = new int[n];
for(int i = 0; i < n && i < res.size(); i++) {
r[i] = res.get(i);
}
return r;
}
In simple words, you need to compare every new element with the maximum of the three you have at hand, and swap them if needed (and if you swap, max of the three has to be recalculated).
I would use 2 arrays of size 3 each:
arrValues = [aV1 aV2 aV3] (reals)
arrPointers = [aP1 aP2 aP3] (integers)
and a 64 bit integer type, call it maxPointer.
I will outline the algorithm logic, since I am not familiar with Java:
Set arrValues = array[0] array[1] array[2] (three first elements of your array)
Set arrPointers = [0 1 2] (or [1 2 3] if your array starts from 1)
Iterate over the remaining elements. In each loop:
Compare the Element scanned in this iteration with arrValues[maxPointer]
If Element <= arrValues[maxPointer],
remove the maxPointer element,
find the new max element and reset the maxPointer
Else
scan next element
End If
Loop
At termination, arrPointers should have the positions of the three smallest elements.
I hope this helps?
There is an easy way to find the positions of three lowest number in an Array
Example :
int[] arr={3,5,1,2,9,7};
int[] position=new int[arr.length];
for(int i=0;i<arr.length;i++)
{
position[i]=i;
}
for(int i=0;i<arr.length;i++)
{
for(int j=i+1;j<arr.length;j++)
{
if(arr[i]>arr[j]){
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
int tem=position[i];
position[i]=position[j];
position[j]=tem;
}
}
}
System.out.println("Lowest numbers in ascending order");
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
System.out.println("And their previous positions ");
for(int i=0;i<arr.length;i++)
{
System.out.println(position[i]);
}
Output
you can do it in 3 iterations.
You need two extra memory, one for location and one for value.
First iteration, you will keep the smallest value in one extra memory and its location in the second. As you are iterating, you compare every value in the slot with the value slot you keep in the memory, if the item you are visiting is smaller than what you have in your extra value slot, you replace the value as well as the location.
At the end of your first iteration, you will find the smallest element and its corresponding location.
You do the same for second and third smallest.

find second and third maximum element array java

I have to find 1st, 2nd, and 3rd largest array. I know I could simply sort it and return array[0], array[1], array[3]. But the problem is, i need the index, not the value.
For example if i have float[] listx={8.0, 3.0, 4.0, 5.0, 9.0} it should return 4, 0, and 3.
Here's the code I have but it doesn't work:
//declaration max1-3
public void maxar (float[] listx){
float maxel1=0;
float maxel2=0;
float maxel3=0;
for (int i=0; i<listx.length; i++){
if(maxel1<listx[i])
{maxel1=listx[i];
max1=i;
}
}
listx[max1]=0; //to exclude this one in nextsearch
for (int j=0; j<listx.length; j++){
if(listx[j]>maxel2)
{maxel2=listx[j];
max2=j;
}
}
listx[max2]=0;
for (int k=0; k<listx.length; k++){
if(listx[k]>maxel3)
{maxel3=listx[k];
max3=k;
}
}
}
I get max1 right but after that all the elements turns to 0. hence max2 and max3 become 0. Please suggest me what is wrong with this solution. Thank you.
You can find the three elements using a single loop, and you don't need to modify the array.
When you come across a new largest element, you need to shift the previous largest and the previous second-largest down by one position.
Similarly, when you find a new second-largest element, you need to shift maxel2 into maxel3.
Instead of using the three variables, you might want to employ an array. This will enable you to streamline the logic, and make it easy to generalize to k largest elements.
Make 3 passes over array: on first pass find value and 1st index of maximum element M1, on second pass find value and 1st index of maximum element M2 which is lesser than M1 and on third pass find value/1st index M3 < M2.
Try this code it will work :)
public class Array
{
public void getMax( double ar[] )
{
double max1 = ar[0]; // Assume the first
double max2 = ar[0]; // element in the array
double max3 = ar[0]; // is the maximum element.
int ZERO = 0;
// Variable to store inside it the index of the max value to set it to zero.
for( int i = 0; i < ar.length; i++ )
{
if( ar[i] >= max1)
{
max1 = ar[i];
ZERO = i;
}
}
ar[ZERO] = 0; // Set the index contains the 1st max to ZERO.
for( int j = 0; j < ar.length; j++ )
{
if( ar[j] >= max2 )
{
max2 = ar[j];
ZERO = j;
}
}
ar[ZERO] = 0; // Set the index contains the 2st max to ZERO.
for( int k = 0; k < ar.length; k++ )
{
if( ar[k] >= max3 )
{
max3 = ar[k];
ZERO = k;
}
}
System.out.println("1st max:" + max1 + ", 2nd: " +max2 + ",3rd: "+ max3);
}
public static void main(String[] args)
{
// Creating an object from the class Array to be able to use its methods.
Array array = new Array();
// Creating an array of type double.
double a[] = {2.2, 3.4, 5.5, 5.5, 6.6, 5.6};
array.getMax( a ); // Calling the method that'll find the 1st max, 2nd max, and and 3rd max.
}
}
I suggest making a single pass instead of three for optimization. The code below works for me. Note that the code does not assert that listx has at least 3 elements. It is up to you to decide what should happen in case it contains only 2 elements or less.
What I like about this code is that it only does one pass over the array, which in its best case would have faster running time compared to doing three passes, with a factor proportionate to the number of elements in listx.
Assume i1, i2 and i3 store the indices of the three greatest elements in listx, and i0 is one of i1, i2 and i3 that points to the smallest element. In the beginning, i1 = i2 = i3 because we haven't found the largest elements yet. So let i0 = i1. If we find a new index j such that that listx[j] > listx[i0], we set i0 = j, replacing that old index with an index that leads to a greater element. Then we find the index among i1, i2 and i3 that now leads to the smallest element of out the three, so that we can safely discard that one in case a new large element comes along.
Note: This code is in C, so translate it to Java if you want to use it. I made sure to use similar syntax to make that easier. (I wrote it in C because I lacked a Java testing environment.)
void maxar(float listx[], int count) {
int maxidx[3] = {0};
/* The index of the 3rd greatest element
* in listx.
*/
int max_3rd = 0;
for (int i = 0; i < count; i++) {
if (listx[maxidx[max_3rd]] < listx[i]) {
/* Exchange 3rd greatest element
* with new greater element.
*/
maxidx[max_3rd] = i;
/* Find index of smallest maximum. */
for (int j = (max_3rd + 1) % 3; j != max_3rd; j = (j + 1) % 3) {
if (listx[maxidx[j]] < listx[maxidx[max_3rd]]) {
max_3rd = j;
}
}
}
}
/* `maxidx' now contains the indices of
* the 3 greatest values in `listx'.
*/
printf("3 maximum elements (unordered):\n");
for (int i = 0; i < 3; i++) {
printf("index: %2d, element: %f\n", maxidx[i], listx[maxidx[i]]);
}
}
public class ArrayExample {
public static void main(String[] args) {
int secondlargest = 0;
int thirdLargest=0;
int largest = 0;
int arr[] = {5,4,3,8,12,95,14,376,37,2,73};
for (int i = 0; i < arr.length; i++) {
if (largest < arr[i]) {
secondlargest = largest;
largest = arr[i];
}
if (secondlargest < arr[i] && largest != arr[i])
secondlargest = arr[i];
if(thirdLargest<arr[i] && secondlargest!=arr[i] && largest!=arr[i] && thirdLargest<largest && thirdLargest<secondlargest)
thirdLargest =arr[i];
}
System.out.println("Largest number is: " + largest);
System.out.println("Second Largest number is: " + secondlargest);
System.out.println("third Largest number is: " + thirdLargest);
}
}
def third_mar_array(arr):
max1=0
max2=0
max3=0
for i in range(0,len(arr)-1):
if max1<arr[i]:
max1=arr[i]
max_in1=i
arr[max_in1]=0
for j in range(0,len(arr)-1):
if max2<arr[j]:
max2=arr[j]
max_in2=j
arr[max_in2]=0
for k in range(0,len(arr)-1):
if max3<arr[k]:
max3=arr[k]
max_in3=k
#arr[max_in3]=0
return max3
n=[5,6,7,3,2,1]
f=first_array(n)
print f
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testcase = sc.nextInt();
while (testcase-- > 0) {
int sizeOfArray = sc.nextInt();
int[] arr = new int[sizeOfArray];
for (int i = 0; i < sizeOfArray; i++) {
arr[i] = sc.nextInt();
}
int max1, max2, max3;
max1 = 0;
max2 = 0;
max3 = 0;
for (int i = 0; i < sizeOfArray; i++) {
if (arr[i] > max1) {
max3 = max2;
max2 = max1;
max1 = arr[i];
}
else if (arr[i] > max2) {
max3 = max2;
max2 = arr[i];
}
else if (arr[i] > max3) {
max3 = arr[i];
}
}
System.out.println(max1 + " " + max2 + " " + max3);
}
}
}

Categories