Time spent here:

Monday, 4 January 2016

Db & His Crush:

Problem Statement
Db proposed his Crush on phone call . His crush has a lot of attitude as she is very beautiful .She gives him a question and says that I will accept your proposal if you will give correct answer of my question. As Db loves his crush a lot so he is asking for help .Now your task is to help Db. The question asked by his crush is: Given an array of n elements .You have to count the minimum number of replacement to make all numbers in array similar. The range of every element of array is from 1 to 3 inclusive.
Input Format
The first line contains an integer n (1 ≤ n ≤ 10^6).
The second line contains n elements.
Output Format
Print the minimum number of replacement to make all numbers in array similar.
Sample Input
9
1 3 2 2 2 1 1 2 3
Sample Output
5
Explanation
We can replace all 1 and 3 with 2 so the total count is 5.

SOLUTION in PYTHON:

n = input()
d = {1:0, 2:0, 3:0}
a = map(int, raw_input().split())
for ii in a: d[ii]+=1
print min(d[1]+d[2], min(d[1]+d[3], d[2] + d[3]))

SOLUTION IN C++:

cin >> n;
for (int i=0;i<n;i++){
    cin >> x;
    a[x]++;
}
int ans=a[2]+a[3];
ans=min(ans,a[1]+a[3]);
ans=min(ans,a[1]+a[2]);
cout << ans;
 }

No comments:

Post a Comment