Equalize the Array Hackerrank problem solution

Equalize the Array Hackerrank

Karl has an array of n integers defined as A=a0,a1,….,an-1. In one operation, he can delete any element from the array.

Karl wants all the elements of the array to be equal to one another. To do this, he must delete zero or more elements from the array. Find and print the minimum number of deletion operations Karl must perform so that all the array’s elements are equal.

Input Format

The first line contains an integer, n, denoting the number of elements in array A.
The next line contains n space-separated integers where element corresponds to array element ai (0Constraints
1<=n<100 1<=ai <=100 Output Format

Print a single integer denoting 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 A=[3,3,2,1,3] . If we delete a2=2 and a3=1, all of the elements in the resulting array, A’=[3,3,3], will be equal. Deleting these 2 elements is minimal because our only other options would be to delete 4elements to get an array of either [1] or [2]. Thus, we print 2 on a new line, as that is the minimum number of deletions resulting in an array where all elements are equal.

Solution

Equalize the Array Hackerrank

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int n,i,j,max=0,cnt;
    scanf("%d",&n);
    int a[n];
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(i=0;i<n;i++)
        {
        cnt=0;
        
        for(j=0;j<n;j++)
            {
            if(a[i]==a[j])
                cnt++;
             }
       
        if(cnt>max)
            max=cnt;
    }
    printf("%d",n-max);

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}
#include <bits/stdc++.h>

using namespace std;

int n, x, h[105];

int main() {
    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        scanf("%d", &x);
        h[x]++;
    }
    printf("%d\n", n - *max_element(h + 1, h + 100 + 1));
}
from collections import Counter

n = input()
A = map(int,raw_input().strip().split())

cts = Counter(A)
print n-max(cts.values())
# Enter your code here. Read input from STDIN. Print output to STDOUT
n= int(raw_input())
from collections import Counter
c=Counter(map(int,raw_input().split()))

l=list(c.values())
print sum(l)-max(l)
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] counts = new int[101];
        for (int i = 0; i < n; i++) {
            counts[sc.nextInt()]++;
        }
        Arrays.sort(counts);
        System.out.println(n-counts[100]);
    }
}

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.