Finding two non-subsequent elements in array which sum is minimal

前端 未结 13 1019
小蘑菇
小蘑菇 2021-02-05 03:40

Intro: As far as I could search, this question wasn\'t asked in SO yet.
This is an interview question.
I am not even specifically looking for a code sol

13条回答
  •  南方客
    南方客 (楼主)
    2021-02-05 03:49

    I don't know if my solution is correct because I just tested it with the data in the OP, and I don't even know if this is better or worse than the other ideas but I wanted to try it.

    static void printMinimalSum(int[] A) {  
        // Looking for mins so we init this with max value
        int[] mins = new int[]{Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE};
        // Indices, used just to print the solution
        int[] indices = new int[]{-1, -1, -1};
        // If the array has length 5 then there's only one solution with the 2nd and 4th elements
        if (A.length == 5) {
            mins[0] = A[1];
            indices[0] = 1;
            mins[1] = A[3];
            indices[1] = 3;
        } else {        
            // Loop on the array without considering the first and the last element
            for (int i = 1; i < A.length - 1; i++) {
                // We consider each element which is smaller than its neighbours
                if ((i == 1 && A[i] < A[i + 1]) // 1: first element, compare it with the second one
                        || (i == A.length - 2 && A[i] < A[i - 1]) // 2: last element, compare it with the previous one
                        || (A[i] < A[i + 1] && A[i] < A[i - 1])) { // 3: mid element, compare it with both neighbors
                    // If the element is "legal" then we see if it's smaller than the 3 already saved
                    if (A[i] < mins[0]) {
                        mins[0] = A[i];
                        indices[0] = i;
                    } else if (A[i] < mins[1]) {
                        mins[1] = A[i];
                        indices[1] = i;
                    } else if (A[i] < mins[2]) {
                        mins[2] = A[i];
                        indices[2] = i;
                    }
                }
            }
        }     
        // Compute the 3 sums between those 3 elements
        int[] sums = new int[]{Math.abs(mins[0]+mins[1]), Math.abs(mins[0]+mins[2]), Math.abs(mins[1]+mins[2])};
        // Find the smaller sum and print it
        if (sums[0] < sums[1] || sums[0] < sums[2]){
            System.out.println("Sum = " + sums[0] + " (elements = {" + mins[0] + "," + mins[1] + "}, indices = {" + indices[0] + "," + indices[1] + "}");
        } else if (sums[1] < sums[0] || sums[1] < sums[2]){
            System.out.println("Sum = " + sums[1] + " (elements = {" + mins[0] + "," + mins[2] + "}, indices = {" + indices[0] + "," + indices[2] + "}");
        } else {
            System.out.println("Sum = " + sums[2] + " (elements = {" + mins[1] + "," + mins[2] + "}, indices = {" + indices[1] + "," + indices[2] + "}");
        }
    }
    
    public static void main(String[] args) {
        printMinimalSum(new int[]{5, 2, 4, 6, 3, 7});
        printMinimalSum(new int[]{1, 2, 3, 3, 2, 1});
        printMinimalSum(new int[]{4, 2, 1, 2, 4});
        printMinimalSum(new int[]{2, 2, 1, 2, 4, 2, 6});
    }
    

    Output is:

    Sum = 5 (elements = {2,3}, indices = {1,4}
    Sum = 4 (elements = {2,2}, indices = {1,4}
    Sum = 4 (elements = {2,2}, indices = {1,3}
    Sum = 3 (elements = {1,2}, indices = {2,5}
    

    which seems fine.

提交回复
热议问题