BITFRENZY

a blog dedicated to technology and programming to shape nation's future.

Breaking

Post Top Ad

Thursday, June 6, 2019

Equalise the array Hackerrank Solution in c


Karl has an array of integers. He wants to reduce the array until all remaining elements are equal. Determine the minimum number of elements to delete to reach his goal.
For example, if his array is , we see that he can delete the  elements  and  leaving . He could also delete both twos and either the  or the , but that would take  deletions. The minimum number of deletions is .
Function Description
Complete the equalizeArray function in the editor below. It must return an integer that denotes the minimum number of deletions required.
equalizeArray has the following parameter(s):
  • arr: an array of integers
Input Format
The first line contains an integer , the number of elements in .
The next line contains  space-separated integers .
Constraints
Output Format
Print a single integer that denotes the minimum number of elements Karl must delete for all elements in the array to be equal.
Sample Input
5
3 3 2 1 3
Sample Output
2   
Explanation
Array . If we delete  and , all of the elements in the resulting array, , will be equal. Deleting these  elements is minimal. Our only other options would be to delete  elements to get an array of either  or .

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
int c=0;
int p=0;
for(int i=0;i<n;i++)
{
c=0;
for(int j=0;j<n;j++)
{
if(a[i]==a[j])
c++;
}
if(c>p)
p=c;
}
printf("%d",(n-p));
return 0;
}


2 comments:

Post Top Ad

Your Ad Spot

Pages