Skip to content

Instantly share code, notes, and snippets.

@alexshavelev
Created November 11, 2014 21:29
Show Gist options
  • Save alexshavelev/4881620f39d8a2a09003 to your computer and use it in GitHub Desktop.
Save alexshavelev/4881620f39d8a2a09003 to your computer and use it in GitHub Desktop.
javarush.test.second
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.*;
/* Задача по алгоритмам
Написать метод, который возвращает минимальное и максимальное числа в массиве.
*/
public class Exted {
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 = getMinimumAndMaximum(data);
System.out.println("Minimum is " + result.x);
System.out.println("Maximum is " + result.y);
}
public static Pair<Integer, Integer> getMinimumAndMaximum(int[] array)
{
int min = array[0];
int max = array[0];
if (array == null || array.length == 0)
{
return new Pair<Integer, Integer>(null, null);
}
else
{
for (int i = 0; i < array.length;i++){
if(min > array[i]){min = array[i];}
if (max < array[i]){ max = array[i];}
}
}
return new Pair<Integer, Integer>(min, max);
}
public static class Pair<X, Y>
{
public X x;
public Y y;
public Pair(X x, Y y)
{
this.x = x;
this.y = y;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment