Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javarush.test.level12.lesson12.bonus03;
- /* Задача по алгоритмам
- Написать метод, который возвращает минимальное число в массиве и его позицию (индекс).
- */
- 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("Minimum is " + result.x);
- System.out.println("Index of 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);
- }
- //Напишите тут ваше решение
- else {
- int minimum = array[0];
- int index = 0;
- for (int temp = 1; temp < array.length; temp++){
- if (array[temp-1]< minimum) {
- minimum = array [temp - 1];
- index = temp - 1;
- }
- }
- return new Pair<Integer, Integer>(minimum, 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;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement