Blogger

Delete comment from: Java67

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class maxMinimumArray {

public static void main(String[] args) {

int[] values = {-20, 34, 21, -87, 92};

int[] sortedArr = sortValues(values);

Map results = maxMinArr(sortedArr);

for(Map.Entry entry : results.entrySet()) {
System.out.println(entry.getKey() + " => " + entry.getValue());
}

}

public static int[] sortValues(int[] arr) {
// sort in asc first (any sort algo will do depending on the complexity you want
// going with bubble sort
for (int i = 0; i < arr.length; i++) {
for (int j = 1; j < arr.length; j++) {
if (arr[j - 1] > arr[j]) {
int temp = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}

public static Map maxMinArr(int[] arr){
Map result = new HashMap<>();

result.put("MinimumValue", arr[0]);
result.put("MaximumValue", arr[arr.length - 1]);

return result;
}

}

Oct 2, 2021, 4:07:26 PM


Posted to How to find largest and smallest number from integer array - Java Solution

Google apps
Main menu