CODE CHEF FEBRUARY CHALLENGE (2 - 11) FEB
MAX COUNT PROBLEM
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 2 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.
SO I HAVE WRITTEN THE SIMPLE CODE FOR THIS......JUST ENJOY...PROGRAMMING...
#include<iostream> #include<stdio.h> #include<stdlib.h> using namespace std; int main() { int T = 0; int A[100]; int N = 0 , index = 0 , V = 0; int C = 0 ,count = 0; scanf("%d",&T); cout<<""<<endl; for(int i = 0 ; i < T ; i++) { scanf("%d",&N); cout<<""<<endl; for(int j = 0 ; j < N ; j++) { cin>>A[j]; } for(int k = 0 ; k < N ; k++) { count = 0; if(A[k] != 0) for(int o = 0 ; o < N ; o++) { if(A[k] == A[o]) { count++; } } if(V < count) { C = count; index = k; V = A[k]; } else if(C == count) { if(A[index] > A[k]) { index = k; V = A[k]; } } } cout<<V<<" "<<C<<endl; } return 0; }