I often would rather stare at the code until I see the issue, but this one it taking me too long.
It is complaining that: The Solution class must have two methods. ( Main and getMinimumAndIndex) - Still an issue
- It is complaining: The getMinimumAndIndex() method must return the minimum of the array and its position (index).
Updated: I figured this one out while writing the question: IntStream.range(1 vs 0... that corrected it)
I get the right answer: ( I think )
The minimum is -8
The index of the minimum element is 5
package com.codegym.task.task12.task1233;
/*
The isomorphs are coming
*/
import java.util.Arrays;
import java.util.stream.IntStream;
public class Solution {
public static void main(String[] args) throws Exception {
int[] data = new int[]{1, 2, 3, 5, -2, -8, 0, 77, 5, 5};
Pair<Integer, Integer> result = getMinimumAndIndex(data);
System.out.println("The minimum is " + result.x);
System.out.println("The index of the minimum element is " + result.y);
}
public static Pair<Integer, Integer> getMinimumAndIndex(int[] array) {
if (array == null || array.length == 0) {
return new Pair<Integer, Integer>(null, null);
}
Integer min = Arrays.stream(array).min().getAsInt();
Integer index = IntStream.range(0, array.length).filter(i -> array[i] == min).findFirst().getAsInt();
return new Pair<Integer, Integer>(min, index);
}
public static class Pair<X, Y> {
public X x;
public Y y;
public Pair(X x, Y y) {
this.x = x;
this.y = y;
}
}
}