1. 程式人生 > >2016騰訊實習生筆試程式設計題:有趣的數字

2016騰訊實習生筆試程式設計題:有趣的數字

import java.util.*;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext())
        {
            int n = sc.nextInt();
            int[] a = new int[n];
            for(int i=0;i<n;i++)
            {
                a[i] = sc.nextInt();
            }

            Arrays.sort(a);
            if(a[0]==a[n-1])
            {
                int temp = (n*(n-1))/2;
                System.out.println(temp + " " + temp);
                continue;
            }

            Map<Integer,Integer> map = new TreeMap<Integer,Integer>();
            for(int i=0;i<n;i++)
            {
                if(map.containsKey(a[i]))
                {
                    map.put(a[i],map.get(a[i])+1);
                }

                else
                {
                    map.put(a[i],1);
                }
            }

            int minres = 0;
            if(map.size()==n)
            {
                int min = Math.abs(a[1]-a[0]);
                for(int i=2;i<n;i++)
                {
                    int temp = Math.abs(a[i]-a[i-1]);
                    if(temp == min)
                    {
                        minres++;
                    }

                    else if(temp<min)
                    {
                        min = temp;
                        minres = 1;
                    }
                }
            }

            else
            {
                for(Integer key:map.keySet())
                {
                    int val = map.get(key);
                    if(val>1)
                    {
                        minres += (val*(val-1))/2;
                    }
                }
            }

            int maxres = 0;
            List<Integer> keyset = new ArrayList<Integer>(map.keySet());
            int val1 = map.get(keyset.get(0));
            int val2 = map.get(keyset.get(keyset.size()-1));
            maxres = val1 * val2;
            System.out.println(minres+" "+maxres);
        }
    }
}