0

I've been given an array. I need to get the minimum value from it, and then return the value's position in the array. I'm newer to Java, and I only really have experience in Python. Here is my code so far.

public static int minPosition (int[] list) {
    int currMin = list[0];
    int index = list.length-1;
    int currPos = 0;

    while (index >= 0){
        if (currMin > list[index])
            currMin = list[index];
        if (currMin > list[index])
            currPos = index;

        index--;

    }
    return currPos;
}

These are my arrays that get called automatically.

minPosition(new int[] { -7 }));
minPosition(new int[] { 1, -4, -7, 7, 8, 11 }));
minPosition(new int[] { -13, -4, -7, 7, 8, 11 }));
minPosition(new int[] { 1, -4, -7, 7, 8, 11, -9 }));

Thank you in advance for the advice.

5 Answers 5

3
    if (currMin > list[index])
        currMin = list[index];
    if (currMin > list[index])
        currPos = index;

If the first if condition is true, then by the time the second condition is checked, curMin will be exactly equal to list[index], so it will never be greater than...

You might want

     if (currMin > list[index]) {
        currMin = list[index];
        currPos = index;
     }
Sign up to request clarification or add additional context in comments.

1 Comment

Never-mind you were correct I had a small mistake further down in my code. Thank you very much!
2

You can add this method and pass in an integer array. It will return the index position of the integer with the lowest value in the array.

public static int getMinIndex(int[] array) {
    int minIndex = -1;
    int currentMinValue = Integer.MAX_VALUE;
    for(int i =0; i< array.length; i++) {
        if(array[i] < currentMinValue) {
            minIndex = i;
            currentMinValue = array[i];
        }
    }
    return minIndex;
}

Comments

0

You could do this with streams:

IntStream(0, list.length).min((i1, i2) -> list[i1] - list[i2]).get();

Comments

0

This solution does not necessitate keeping track of the current minimum. This makes the code easier to follow, and leaves less room for errors. It also starts searching from the beginning, which your original code is not doing. (I am not sure why that is so, since you would presumably want the first index that has a minimum value, not the last.)

public static int minPosition (int[] list) {
  if( list == null || list.length = 0){
    return -1;
  }

  int minPos = 0;

  for(int i = 0; i < list.length; i++){
    if(list[i] < list[minPos]){
      minPos = i;
    }
  }
  return minPos;
}

2 Comments

Question from a not knowledgeable enough person. Wouldn't it be faster to store the minimun value to a variable instead of accessing it in every iteration with list[minPos]?
@Reti43 It is faster, but indexing an array will happen in O(1) time, i.e., no matter how large the array is, it will always take the same amount of time. With the sheer speed of computing today, I prefer accuracy to marginal speed improvement.
0

Assuming a not null array, you can first sort the list and then return the first from sorted list. This will not be as efficient as some examples above but the code will be the shorter.

public static int minPosition (int[] list) {
        Arrays.sort(list);
        return list[0];
}

1 Comment

The question asks for the position of the minimum value, not the value. You could rewrite your answer though, as return new ArrayList<Integer>(list).indexOf(list[0]);.

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.