In: Computer Science
Find the second minimum of an integer array?
//this is the template we follow. Answer must run in linear time (i.e. no nested loops).
package findsecondminimumtest;
import java.util.Arrays;
import java.util.NoSuchElementException;
public class FindSecondMinimumTest {
/**
* Find the second minimum of an integer array
*
* @param a is the array
* @return the second minimum if array has at least two elements and
it
* indeed has a second minimum. If array length is less than two, it
throws
* IllegalArgumentException. If there is no second minimum (eg. if
your
* array if {1, 1, 1}, there is no second minimum), method
throws
* NoSuchElementException.
*/
static int findSecondMin(int[] a) {
// write your code here
int smin = 0;
return smin;
}
public static void main(String[] args) {
/*
DO NOT CHANGE ANYTHING in the main method!!!. If your code works,
properly,
it should print true for all cases except the last two. Last two
check for exception case.
It is not hard to understand why it works that way.
*/
int[] a1 = {1, 3, 2, 1}; // 2
int[] a2 = {1, 3, 2}; // 2
int[] a3 = {1, 2, 1, 2}; // 2
int[] a4 = {3, 6, 5, 8, 10, 20, 15}; // 5
int[] a5 = {1, 1, 2, 3, 4, 4}; // 2
int[] a6 = {1, 4, 10, 4, 2}; // 2
int[] a7 = {10, 1, 2, 3, 4, 5}; // 2
int[] a8 = {1, 1, 1, 2, 3}; // 2
int[] a9 = {0, -2, 5, 6}; // 0
int[] a10 = {1, 2, 3, 4, 5, 3, 5, 6}; // 2
int[] a11 = {40, 50, 60, 10, 20, 30}; // 20
int[] a13 = {1, 2, 5, 3, 5}; // 2
int[] a14 = {1, 2, 5, 5, 5}; // 2
int[] a15 = {10, 1, 2, 3, 4, 5, 6, 1}; // 2
int[] a16 = {1, 2, 3, 4, 3, 6}; // 2
int[] a17 = {1, 2, 3, 4, 99, 5, 6}; // 2
int[] a18 = {123, -17, -5, 1, 2, 3, 12, 43, 45}; // -5
int[] a19 = {3, 5, 67, 98, 3}; // 5
int[] a12 = {1, 1, 1}; // Should throw NoSuchElementException
exception
int[] a20 = {1}; // Should throw IllegalArgumentException
System.out.println(Arrays.toString(a1) + ": " +
(findSecondMin(a1) == 2));
System.out.println(Arrays.toString(a2) + ": " + (findSecondMin(a2)
== 2));
System.out.println(Arrays.toString(a3) + ": " + (findSecondMin(a3)
== 2));
System.out.println(Arrays.toString(a4) + ": " + (findSecondMin(a4)
== 5));
System.out.println(Arrays.toString(a5) + ": " + (findSecondMin(a5)
== 2));
System.out.println(Arrays.toString(a6) + ": " + (findSecondMin(a6)
== 2));
System.out.println(Arrays.toString(a7) + ": " + (findSecondMin(a7)
== 2));
System.out.println(Arrays.toString(a8) + ": " + (findSecondMin(a8)
== 2));
System.out.println(Arrays.toString(a9) + ": " + (findSecondMin(a9)
== 0));
System.out.println(Arrays.toString(a10) + ": " +
(findSecondMin(a10) == 2));
System.out.println(Arrays.toString(a11) + ": " +
(findSecondMin(a11) == 20));
System.out.println(Arrays.toString(a13) + ": " +
(findSecondMin(a13) == 2));
System.out.println(Arrays.toString(a14) + ": " +
(findSecondMin(a14) == 2));
System.out.println(Arrays.toString(a15) + ": " +
(findSecondMin(a15) == 2));
System.out.println(Arrays.toString(a16) + ": " +
(findSecondMin(a16) == 2));
System.out.println(Arrays.toString(a17) + ": " +
(findSecondMin(a17) == 2));
System.out.println(Arrays.toString(a18) + ": " +
(findSecondMin(a18) == -5));
System.out.println(Arrays.toString(a19) + ": " +
(findSecondMin(a19) == 5));
try {
System.out.println(Arrays.toString(a12) + ": " +
(findSecondMin(a12) == 1));
} catch (Exception e) {
System.out.println(e.toString());
}
try {
System.out.println(Arrays.toString(a20) + ": " +
(findSecondMin(a20) == 1));
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
package findsecondminimumtest; import java.util.Arrays; import java.util.NoSuchElementException; public class FindSecondMinimumTest { /** * Find the second minimum of an integer array * * @param a is the array * @return the second minimum if array has at least two elements and it * indeed has a second minimum. If array length is less than two, it throws * IllegalArgumentException. If there is no second minimum (eg. if your * array if {1, 1, 1}, there is no second minimum), method throws * NoSuchElementException. */ static int findSecondMin(int[] a) { if (a.length < 2) throw new NoSuchElementException(); int min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE; for (int i = 0; i < a.length; i++) { if (a[i] < min1) min1 = a[i]; } int count = 0; for (int i = 0; i < a.length; i++) { if (a[i] < min2 && a[i] != min1) { min2 = a[i]; ++count; } } if (count == 0) throw new NoSuchElementException(); return min2; } public static void main(String[] args) { /* DO NOT CHANGE ANYTHING in the main method!!!. If your code works, properly, it should print true for all cases except the last two. Last two check for exception case. It is not hard to understand why it works that way. */ int[] a1 = {1, 3, 2, 1}; // 2 int[] a2 = {1, 3, 2}; // 2 int[] a3 = {1, 2, 1, 2}; // 2 int[] a4 = {3, 6, 5, 8, 10, 20, 15}; // 5 int[] a5 = {1, 1, 2, 3, 4, 4}; // 2 int[] a6 = {1, 4, 10, 4, 2}; // 2 int[] a7 = {10, 1, 2, 3, 4, 5}; // 2 int[] a8 = {1, 1, 1, 2, 3}; // 2 int[] a9 = {0, -2, 5, 6}; // 0 int[] a10 = {1, 2, 3, 4, 5, 3, 5, 6}; // 2 int[] a11 = {40, 50, 60, 10, 20, 30}; // 20 int[] a13 = {1, 2, 5, 3, 5}; // 2 int[] a14 = {1, 2, 5, 5, 5}; // 2 int[] a15 = {10, 1, 2, 3, 4, 5, 6, 1}; // 2 int[] a16 = {1, 2, 3, 4, 3, 6}; // 2 int[] a17 = {1, 2, 3, 4, 99, 5, 6}; // 2 int[] a18 = {123, -17, -5, 1, 2, 3, 12, 43, 45}; // -5 int[] a19 = {3, 5, 67, 98, 3}; // 5 int[] a12 = {1, 1, 1}; // Should throw NoSuchElementException exception int[] a20 = {1}; // Should throw IllegalArgumentException System.out.println(Arrays.toString(a1) + ": " + (findSecondMin(a1) == 2)); System.out.println(Arrays.toString(a2) + ": " + (findSecondMin(a2) == 2)); System.out.println(Arrays.toString(a3) + ": " + (findSecondMin(a3) == 2)); System.out.println(Arrays.toString(a4) + ": " + (findSecondMin(a4) == 5)); System.out.println(Arrays.toString(a5) + ": " + (findSecondMin(a5) == 2)); System.out.println(Arrays.toString(a6) + ": " + (findSecondMin(a6) == 2)); System.out.println(Arrays.toString(a7) + ": " + (findSecondMin(a7) == 2)); System.out.println(Arrays.toString(a8) + ": " + (findSecondMin(a8) == 2)); System.out.println(Arrays.toString(a9) + ": " + (findSecondMin(a9) == 0)); System.out.println(Arrays.toString(a10) + ": " + (findSecondMin(a10) == 2)); System.out.println(Arrays.toString(a11) + ": " + (findSecondMin(a11) == 20)); System.out.println(Arrays.toString(a13) + ": " + (findSecondMin(a13) == 2)); System.out.println(Arrays.toString(a14) + ": " + (findSecondMin(a14) == 2)); System.out.println(Arrays.toString(a15) + ": " + (findSecondMin(a15) == 2)); System.out.println(Arrays.toString(a16) + ": " + (findSecondMin(a16) == 2)); System.out.println(Arrays.toString(a17) + ": " + (findSecondMin(a17) == 2)); System.out.println(Arrays.toString(a18) + ": " + (findSecondMin(a18) == -5)); System.out.println(Arrays.toString(a19) + ": " + (findSecondMin(a19) == 5)); try { System.out.println(Arrays.toString(a12) + ": " + (findSecondMin(a12) == 1)); } catch (Exception e) { System.out.println(e.toString()); } try { System.out.println(Arrays.toString(a20) + ": " + (findSecondMin(a20) == 1)); } catch (Exception e) { System.out.println(e.toString()); } } }