[Coding Club] Minimum Maximum Array Problem
How to tackle an array problem : –
Understanding the problem is very important, read problem 2-3 times, give time to understand the problem, Check input and expected output for further clarity.
- As we can see the expected output requires minimum and maximum value of an array of integers
- for calculating minimum and maximum we can use 2 approaches
- Greedy algorithm
- Sorting ARRAY approach
- Greedy algorithm suggest that take element 1 by 1 and compare with other elements of an array, take the minimum of them, and continue till last
- Sorting Array approach – it’s simpler than the previous one, we all know if we sort an array, the first element of that array will be the minimum and the last element of the array will be the maximum
You can check the below code written using sorting array approach
import java.util.Arrays;
public class test1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] intArray = new int[]{ 1000,11,445,1,330,3000 };
minmax(intArray);
}
private static void minmax(int[] intArray) {
// TODO Auto-generated method stub
Arrays.sort(intArray);
System.out.println(intArray[0] +" "+ intArray[intArray.length-1]);
}
}
Output Screen :-