3

I'm trying to find the first index of the minimum value based on the first column in a 2d array, this is my function:

n is the number of rows and k is 0 at the beginning.

public int findMinRecursive(int[][] array, int n, int k) {
    int min;
    if (n == 1) {
        return array[0][k];

    } else {
        min = findMinRecursive(array, n - 1, k);
        if(min == array[n - 1][k]){
            k++;
        }
        if (min < array[n - 1][k]) {
            return min;
        }
        else {
            return array[n - 1][k];
        }
    }
}

In case there is more than one minimum, it shows the one with the lowest value in the next column. This function works but I need to get the first index of my array too and I don't know how. This how I call the function:

int min = sortMyArray.findMinRecursive(array, n, 0);
System.out.println("Min = " + min);
3
  • 1
    The minimum value in a 2d array has two indices. You seem to be finding the minimum value for the kth column. Only you locally modify k. How are you calling this? Commented May 21, 2018 at 21:30
  • I edit my question so it would be clear. Commented May 21, 2018 at 21:47
  • In its current state this question is hard to understand. Could you please edit your question to include example input and what the output should be. See: minimal reproducible example. Commented May 21, 2018 at 22:03

1 Answer 1

1

find the first index of the minimum value in a 2d array

Means (for me): In an integer Array containing a minimum element A[minI][minJ] return minI.

Now recursion can be applied on two levels (in a two-dimensional array), i recommend to recurse over the "rows" (comparing the min value of each row...and storing the index of "the row") :

Update: now with real recursion, and only comparing the first column:

public class Q50456760 {

    private static final int[][] TEST_A = {{0, 1, 2}, {3, 4, 5}, {6, 7, -1}};

    public static void main(String... args) {
        //        System.out.println(TEST_A);
        System.out.print("Min row index (expected 0): ");
        System.out.println(Q50456760.findMinRowIndexIter(TEST_A, Integer.MAX_VALUE));
        System.out.print("Recursive: ");
        System.out.println(Q50456760.findMinRowIndexRec(TEST_A, 0, Integer.MAX_VALUE, -1));
    }

    private static int findMinRowIndexIter(int[][] arr, int min) {
        int minI = -1;
        for (int i = 0; i < arr.length; i++) {
            int currMin = findMinIter(arr[i], min);
            if (currMin < min) {
                min = currMin;
                minI = i;
            }
        }
        return minI;
    }

    private static int findMinRowIndexRec(int[][] arr, int i, int min, int minI) {
        int currMin = findMinRec(arr[i], 0, min);
        if (currMin < min) {
            min = currMin;
            minI = i;
        }
        i++;
        if (i < arr.length) {
            return findMinRowIndexRec(arr, i, min, minI);
        } else {
            return minI;
        }
    }

    private static int findMinIter(int[] arr, int min) {
        int result = min;
//            for (int aj : arr) {
        if (arr[0] < min) {
            result = arr[0];
        }
//            }
        return result;
    }

    private static int findMinRec(int[] a, int j, int min) {
        if (a[j] < min) {
            min = a[j];
        }
//            j++;
//            if (j < a.length) {
//                return findMinRec(a, j, min);
//            } else {
        return min;
//            }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Your update doesn’t take into account OP’s updated problem statement: "find the first index of the minimum value based on the first column in a 2d array."
ah, that even makes it easier: findMinRec needs to be called on the 1st (0th) element only...then I would expect output 0 for the given TEST_A(!?))
updated regarding first column (and fixed a bug - min needs to be stored, too!)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.