Problem: In this problem, you have to implement the int findMinimum(int[] arr) method, which will traverse the whole array and find the smallest number in the array.
Method: int findMinimum(int[] arr)
input:arr = {9, 2, 3, 6} output: 2
class CheckMinimum
{
public static int findMinimum(int[] arr) {
int temp= arr[0];
for(int i=0; i<arr.length; i ){
if(temp< arr[i 1])
return temp;
else
temp= arr[i 1];
}
return temp;
}
public static void main(String args[]) {
int[] arr = {4,5,0,3,6};
System.out.print("Array : ");
for(int i = 0; i < arr.length; i )
System.out.print(arr[i] " ");
System.out.println();
int min = findMinimum(arr);
System.out.println("Minimum in the Array: " min);
}
}
My code was successful in most of the cases, but I cannot figure out why this one failed. Could anyone kindly explain why?
case:
findMinimum([4,5,0,3,6])
expected output 0
my code output: 4
If I change the position of 0, my code still works fine. Thanks!
CodePudding user response:
This is the problematic part:
if (temp< arr[i 1])
return temp;
else
temp= arr[i 1];
For your array 4,5,0,3,6 it never even comes to 0, since already (temp) 4 < 5 (arr[i 1]) and your function returns 4 and stops. Your function returns too early.
Correct would be:
if (temp > arr[i 1]) temp = arr[i 1]
Put this instead of your whole if...else block.
CodePudding user response:
For a solution that uses Java8 streams you could try:
Arrays.stream(arr).min();
CodePudding user response:
your return statement in if condition is breaking the loop and giving the result. Since first element 4 is less than next element (i.e. 5), it return 4. remove the return statement inside if and it will work.
Also it can be done with built in function like in python - min(sorted(arr))
CodePudding user response:
The @milan answer works to resolve this problem. Pls pay attention that your for
in the findMinimum
method will break too.
If your keep for(int i=0; i<arr.length; i ){
when it iterate with the last element the code will try compare with position 5 but the max index is 4 (0..4)
And will return a Index of bound exception
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
at CheckMinimum.findMinimum(CheckMinimum.java:7)
at CheckMinimum.main(CheckMinimum.java:23)
Please subtract 1
public static int findMinimum(int[] arr) {
int temp = arr[0];
for(int i=0; i < (arr.length - 1); i ){
int current = arr[i 1];
if(temp > current)
temp = current;
}
return temp;
}
You can improve the code changing the for
in main
method
for (int j : arr) System.out.print(j " ");
public static void main(String args[]) {
int[] arr = {4,5,0,3,6};
System.out.print("Array : ");
for (int j : arr) System.out.print(j " ");
System.out.println();
int min = findMinimum(arr);
System.out.println("Minimum in the Array: " min);
}
When you're not handling the index inside the for, you can use the foreach
approach.
So.. You can change too the findMinimun
method for Arrays.stream(arr).min();
But pay attention because this return is an OptionalInt.
OptionalInt min = Arrays.stream(arr).min();
System.out.println("\nMinimum in the Array: " min);
And at last, when you declare an array, it's better to use the [] with the type.
Consider String[] args
instead of String args[]
So... the final code can be something like that
import java.util.Arrays;
import java.util.OptionalInt;
class CheckMinimum {
public static void main(String args[]) {
int[] arr = {4,5,0,3,6};
System.out.print("Array : ");
for (int j : arr) System.out.print(j " ");
OptionalInt min = Arrays.stream(arr).min();
System.out.println("\nMinimum in the Array: " min);
}
}