Question

In: Computer Science

Find the second minimum of an integer array? //this is the template we follow. Answer must...

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());
}
}

}

Solutions

Expert Solution

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());
        }
    }
}


Related Solutions

How could we use Pthreads to find the minimum value in an array without mutex locks?...
How could we use Pthreads to find the minimum value in an array without mutex locks? Would this version be slower? If so, why?
In Java Find the second largest and second smallest element in a given array. You can...
In Java Find the second largest and second smallest element in a given array. You can hardcode/declare the array in your program.
Given a minimum unimodal array of integers, run the binary search algorithm to find the minimum...
Given a minimum unimodal array of integers, run the binary search algorithm to find the minimum element. You need to show the initial and the iteration-level values of the left index, right index and middle index as well as your decisions to reduce the search space in each iteration. 42 39 2 6 9 16 20 28 31 34
(C++) Follow the template given to calculate and print the monthly salary of an employee. We...
(C++) Follow the template given to calculate and print the monthly salary of an employee. We assume the employee works for 50 weeks during the year with an hourly rate of $25. Your program should ask the user the workHoursPerWeek. If it's over 40, then the excess hours (i.e., workHoursPerWeek over 40) are paid with 20% overtime rate. Note: just print out the monthly salary. Example: If I work for 45 hours with a rate of $30/hr, then my weekly...
Divide and conquer approach to find the minimum absolute difference in array A[lo..hi] Input an array...
Divide and conquer approach to find the minimum absolute difference in array A[lo..hi] Input an array A[lo..hi] of n real numbers. Requirement: Shouldn't use a sorting algorithm. Complexity O(nlgn)
Suppose we have an array as follow: var $fruits =new Array(“banana”, “strawberry”, “papaya”, “melon”); Write a...
Suppose we have an array as follow: var $fruits =new Array(“banana”, “strawberry”, “papaya”, “melon”); Write a PHP script which will display the list of fruits in the browser as below: • Fruit 1: banana • Fruit 2: strawberry • Fruit 3: papaya
Respond to the following in a minimum of 175 words: This second week we build our...
Respond to the following in a minimum of 175 words: This second week we build our understanding of ethical accounting by examining specific cases and effects that demonstrate the importance of fostering an ethical “culture” in a company and its various functions. strategic decision-making. In light of the accounting and budgeting systems we studied last week, consider how the costs involved in business performance and decision-making (Chapters 2 through 5 in our text) are tabulated, Discuss the importance of an...
Write a program to produce an array of integer random numbers. Your program should find out...
Write a program to produce an array of integer random numbers. Your program should find out from the user how many numbers to store. It should then generate and store that many random integers (the random numbers must be between 1 and 999 inclusive). The program should then determine the smallest number, the largest number, and the average of all the numbers stored in the array. Finally, it should print out all the numbers on the screen, five numbers to...
This is a combinatorics problem Suppose we wish to find the number of integer solutions to...
This is a combinatorics problem Suppose we wish to find the number of integer solutions to the equation below, where 3 ≤ x1 ≤ 9, 0 ≤ x2 ≤ 8, and 7 ≤ x3 ≤ 17. x1 + x2 + x3 = r Write a generating function for this problem, and use it to solve this problem for r = 20.
C program //In this assignment, we will find the smallest positive integer that // can be...
C program //In this assignment, we will find the smallest positive integer that // can be expressed as a sum of two positive cube numbers in two distinct ways. // More specifically, we want to find the smallest n such that n == i1*i1*i1 + j1*j1*j1, // n == i2*i2*i2 + j2*j2*j2, and (i1, j1) and (i2, j2) are not the same in the sense that // not only (i1, j1) not euqal to (i2, j2), but also (i1, j1)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT