Javatpoint Logo
Javatpoint Logo

Equalize the Array

Problem Statement

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 arr = [ 1, 2, 2, 3 ], we see that he can delete the 2 elements 1 and 3 leaving arr = [ 2, 2 ]. He could also delete both twos and either the 1 or the 3, but that would take 3 deletions. The minimum number of deletions is 2.

Function Description

Complete the equalize Array function in the editor below. It must return an integer that denotes the minimum number of deletions required.

equalize Array has the following parameter(s):

  • arr: an array of integers

Input Format

The first line contains an integer, the number of elements in arr.
The next line contains n space-separated integers arr[ i ].

Constraints

  • 1 <= n < = 100
  • 1 <= arr[i] < = 100

Output Format

Print a single integer that denotes the minimum number of elements that must be deleted to equalize the array.

Sample Input

Sample Output

2

Explanation

Array arr[ 3, 3, 2, 1, 3 ] . If we delete arr[ 2 ] = 2 and arr[ 3 ] = 1, all of the elements in the resulting array, A' = [ 3, 3, 3 ], will be equal. Deleting these 2 elements is minimal. Our only other options would be to delete 4 elements to get an array of either [ 1 ] or [ 2 ].

Code in C of the above problem

Explanation of above code

If we consider the value of n=5;

Declare & initialize variables n, i, j, c1 = 1, c2 = 1, a[100]

Enter no. of elements (n) of an array. Let n = 5

for(i = 0; i < n; i++)

{

scanf(" %d ", &a[i]);

}

Using above for loop insert value in an array a, having n elements, lets array a has a[3, 3, 2, 1, 3] following elements after insertion.

Outer for loop will run from 0 to 4.

for i = 0

C1 = 1

Inner for loop will run for 1 to 4, for j = 1

if(a[i] == a[j]) evaluates to be TRUE, since i = 0 & j = 1 and array a[3, 3, 2, 1, 3]

C1++ => C1 = 2

for j = 2

if(a[i] == a[j]) evaluates to be FALSE, since i = 0 & j = 2 and array a[3, 3, 2, 1, 3]

C1++ => Will not execute

for j = 3

if(a[i] == a[j]) evaluates to be FALSE, since i = 0 & j = 3 and array a[3, 3, 2, 1, 3]

C1++ => Will not execute

for j = 4

if(a[i] == a[j]) evaluates to be TRUE, since i = 0 & j = 4 and array a[3, 3, 2, 1, 3]

C1++ => C1 = 3. NOW j loop will terminate

if(c1 > c2) evaluates to TRUE, since C1 = 3 and C2 = 1

C2 = C1 => C2 = 3

for i = 1

C1 = 1

Inner for loop will run for 2 to 4, for j = 2

if(a[i] == a[j]) evaluates to be FALSE, since i = 1 & j = 2 and array a[3, 3, 2, 1, 3]

C1++ => Will not execute

for j = 3

if(a[i] == a[j]) evaluates to be FALSE, since i = 1 & j = 3 and array a[3, 3, 2, 1, 3]

C1++ => Will not execute

for j = 4

if(a[i] == a[j]) evaluates to be FALSE, since i = 1 & j = 4 and array a[3, 3, 2, 1, 3]

C1++ => C1 = 2

if(c1 > c2) evaluates to FALSE, since C1 = 2 and C2 = 3

C2 = C1 => Will not execute

for i = 2

C1 = 1

Inner for loop will run for 3 to 4, for j = 3

if(a[i] == a[j]) evaluates to be FALSE, since i = 1 & j = 2 and array a[3, 3, 2, 1, 3]

C1++ => Will not execute

for j = 4

if(a[i] == a[j]) evaluates to be FALSE, since i = 1 & j = 3 and array a[3, 3, 2, 1, 3]

C1++ => Will not execute

if(c1 > c2) evaluates to TRUE, since C1 = 1 and C2 = 3

C2 = C1 => Will not execute

for i = 3

C1 = 1

Inner for loop will run for 4 to 4, for j = 4

if(a[i] == a[j]) evaluates to be FALSE, since i = 1 & j = 2 and array a[3, 3, 2, 1, 3]

C1++ => Will not execute

if(c1 > c2) evaluates to FALSE, since C1 = 1 and C2 = 3

C2 = C1 => Will not execute

for i = 4

C1 = 1

Inner loop will not run since 5 < 4 becomes FALSE

if(c1 > c2) evaluates to FALSE, since C1 = 1 and C2 = 3

C2 = C1 => Will not execute

printf(" %d ", n - c2); will print 2, since C2 = 3 and value of n = 5, which shows two elements from any array a[3, 3, 2, 1, 3] to be deleted in order to equalize the array.

If we consider the value of n=4;

Declare & initialize variables n, i, j, c1 = 1, c2 = 1, a[100]

Enter no. of elements (n) of an array. Let n = 4

for(i = 0; i < n; i++)

{

scanf(" %d ", &a[i]);

}

Using above for loop insert value in an array a, having n elements, lets array a has a[1, 2, 2, 3] following elements after insertion.

Outer for loop will run from 0 to 3.

for i = 0

C1 = 1

Inner for loop will run for 1 to 3, for j = 1

if(a[i] == a[j]) evaluates to be FALSE, since i = 0 & j = 1 and array a[1, 2, 2, 3]

C1++ => Will not execute

for j = 2

if(a[i] == a[j]) evaluates to be FALSE, since i = 0 & j = 2 and array a[1, 2, 2, 3]

C1++ => Will not execute

for j = 3

if(a[i] == a[j]) evaluates to be FALSE, since i = 0 & j = 3 and array a[1, 2, 2, 3]

C1++ => Will not execute, NOW j loop will terminate

if(c1 > c2) evaluates to FALSE

C2 = C1 => Will not execute

for i = 1

C1 = 1

Inner for loop will run for 2 to 3, for j = 2

if(a[i] == a[j]) evaluates to be TRUE, since i = 1 & j = 2 and array [1, 2, 2, 3]

C1++ => C1 = 2

for j = 3

if(a[i] == a[j]) evaluates to be FALSE, since i = 1 & j = 3 and array a[1, 2, 2, 3]

C1++ => Will not execute, NOW j loop will terminate

if(c1 > c2) evaluates to TRUE, since C1 = 2 and C2 = 1

C2 = C1 => C2 = 2

for i = 2

C1 = 1

Inner for loop will run for 3 to 3, for j = 3

f(a[i] == a[j]) evaluates to be FALSE, since i = 2 & j = 3 and array a[1, 2, 2, 3]

C1++ => Will not execute

for i = 3

C1 = 1

Inner for loop will not execute.

if(c1 > c2) evaluates to FALSE, since C1 = 1 and C2 = 2

C2 = C1 => Will not execute

printf(" %d ", n - c2); will print 2, since C2 = 2 and value of n = 4, which shows two elements from any array a[3, 3, 2, 1, 3] to be deleted in order to equalize the array







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA