Friday, March 1, 2013

Count of Maximum(CodeChef )

Given an array A of length N, your task is to find the element which repeats in A maximum number of times as well as the
corresponding count. In case of ties, choose the smaller element first.
Input

First line of input contains an integer T, denoting the number of test cases. Then follows description of T cases. Each case
begins with a single integer N, the length of A. Then follow N space separated integers in next line. Assume that 1 <= T
<= 100, 1 <= N <= 100 and for all i in [1..N] : 1 <= A[i] <= 10000
Output
For each test case, output two space separated integers V & C. V is the value which occurs maximum number of times and C is its count.
Example Input:
2
5
1 2 3 2 5
6
1 2 2 1 1 2v Output:
2 2
1 3
Description:
In first case 2 occurs twice whereas all other elements occur only once.
In second case, both 1 and 2 occur 3 times but 1 is smaller than 2.
Solution:
from collections import Counter
def com(N,A):
    num_array=A.split()
    c=Counter(num_array)
    C=max(c.values())
    l1=[]
    for k in c:
        if c[k]==C:
            l1.append(k)
    l2=[int(k) for k in l1]
    V1=min(l2)
    print V1,C

t=input()
for i in range(t):
    N=input()
    A=raw_input()
    com(N,A)
This question is special because i've written everything with a little help from a friend @ stackoverflow.com http://stackoverflow.com/questions/15169474/whats-wrong-with-my-solutioncount-of-maximumcode-chef

No comments:

Post a Comment