We are required in our assignment to find the second smallest integer in one array recursively. However, for the sake of understanding the subject more, I want to do it iter
public static int getSecondSmallest(int[] arr){
int smallest = Integer.MAX_VALUE;
int secondSmallest = Integer.MAX_VALUE;
for(int i=0;i arr[i]){
secondSmallest = smallest;
smallest = arr[i];
}else if (secondSmallest > arr[i] && arr[i] != smallest){
secondSmallest = arr[i];
}
System.out.println(i+" "+smallest+" "+secondSmallest);
}
return secondSmallest;
}
Just gave it a try with some of the test cases and it worked. Please check if it is correct!