Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /* when it is inside the <> it means that we are giving it objects which are already comparable */
- class MinMax<T extends Comparable<T>> {
- T min;
- T max;
- int countMin;
- int countMax;
- int total;
- public MinMax() {
- /* Default Constructor */
- this.countMin = 0;
- this.countMax = 0;
- this.total = 0;
- }
- /*
- 12
- 8
- 8
- 5
- 55
- 13
- 55*/
- public void update(T element) {
- /* Starting scenario*/
- if (total == 0) {
- max = element;
- min = element;
- }
- /* Midway scenario */
- if (min.compareTo(element) > 0) {
- min = element;
- countMin = 1;
- } else if (min.compareTo(element) == 0) {
- countMin++;
- }
- if (max.compareTo(element) < 0) {
- max = element;
- countMax = 1;
- } else if (max.compareTo(element) == 0) {
- countMax++;
- }
- /* Update the total */
- total++;
- }
- public T max() {
- return max;
- }
- public T min() {
- return min;
- }
- @Override
- public String toString() {
- return min + " " + max + " " + (total - countMin - countMax) + "\n";
- }
- }
- public class MinAndMax {
- public static void main(String[] args) throws ClassNotFoundException {
- Scanner scanner = new Scanner(System.in);
- int n = scanner.nextInt();
- MinMax<String> strings = new MinMax<String>();
- for (int i = 0; i < n; ++i) {
- String s = scanner.next();
- strings.update(s);
- }
- System.out.println(strings);
- MinMax<Integer> ints = new MinMax<Integer>();
- for (int i = 0; i < n; ++i) {
- int x = scanner.nextInt();
- ints.update(x);
- }
- System.out.println(ints);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment