Maximum count of equal numbers in an array after performing given operations

Given an array of integers. The task is to find the maximum count of equal numbers in an array after applying the given operation any number of times.

In an operation:

1. Choose two elements of the array a[i], a[j] (such that i is not equals to j) and,
2. Increase number a[i] by 1 and decrease number a[j] by 1 i.e., a[i] = a[i] + 1 and a[j] = a[j] – 1.



Examples:

Input: a = { 1, 4, 1 }
Output: 3
after first step { 1, 3, 2}
after second step { 2, 2, 2}

Input: a = { 1, 2 }
Output: 1

Approach :

  1. Calculate the sum of the array elements.
  2. If the sum is divisible by n, where n is the number of elements in the array then the answer will also be n.
  3. Otherwise, the answer will be n-1.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find the maximum
// number of equal numbers in an array
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the maximum number of
// equal numbers in an array
int EqualNumbers(int a[], int n)
{
    // to store sum of elements
    int sum = 0;
    for (int i = 0; i < n; i++)
        sum += a[i];
  
    // if sum of numbers is not divisible
    // by n
    if (sum % n)
        return n - 1;
  
    return n;
}
  
// Driver Code
int main()
{
    int a[] = { 1, 4, 1 };
  
    // size of an array
    int n = sizeof(a) / sizeof(a[0]);
  
    cout << EqualNumbers(a, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the maximum
// number of equal numbers in an array
  
public class GFG{
      
    // Function to find the maximum number of
    // equal numbers in an array
    static int EqualNumbers(int a[], int n)
    {
        // to store sum of elements
        int sum = 0;
        for (int i = 0; i < n; i++)
            sum += a[i];
  
        // if sum of numbers is not divisible
        // by n
        if (sum % n != 0)
            return n - 1;
  
        return n;
    }
  
      
    // Driver code
    public static void main(String args[])
    {
            int a[] = { 1, 4, 1 };
  
            // size of an array
            int n = a.length;
  
            System.out.println(EqualNumbers(a, n));
    }
    // This code is contributed by ANKITRAI1
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find the maximum
# number of equal numbers in an array
  
# Function to find the maximum 
# number of equal numbers in an array
def EqualNumbers(a, n):
  
    # to store sum of elements
    sum = 0;
    for i in range(n):
        sum += a[i];
  
    # if sum of numbers is not 
    # divisible by n
    if (sum % n):
        return n - 1;
  
    return n;
  
# Driver Code
a = [1, 4, 1 ];
  
# size of an array
n = len(a);
  
print(EqualNumbers(a, n));
  
# This code is contributed by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the maximum 
// number of equal numbers in an array 
using System;
  
class GFG
{
// Function to find the maximum 
// number of equal numbers in an array 
static int EqualNumbers(int []a, int n) 
    // to store sum of elements 
    int sum = 0; 
    for (int i = 0; i < n; i++) 
        sum += a[i]; 
  
    // if sum of numbers is not 
    // divisible by n 
    if (sum % n != 0) 
        return n - 1; 
  
    return n; 
  
// Driver code 
static public void Main ()
{
    int []a = { 1, 4, 1 }; 
  
    // size of an array 
    int n = a.Length; 
  
    Console.WriteLine(EqualNumbers(a, n)); 
  
// This code is contributed by jit_t

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find the maximum
// number of equal numbers in an array
  
// Function to find the maximum 
// number of equal numbers in an array
function EqualNumbers($a, $n)
{
    // to store sum of elements
    $sum = 0;
    for ($i = 0; $i < $n; $i++)
        $sum += $a[$i];
  
    // if sum of numbers is not 
    // divisible by n
    if ($sum % $n)
        return $n - 1;
  
    return $n;
}
  
// Driver Code
$a = array(1, 4, 1 );
  
// size of an array
$n = sizeof($a);
  
echo EqualNumbers($a, $n);
  
// This code is contributed
// by Akanksha Rai

chevron_right


Output:

3


My Personal Notes arrow_drop_up

pawanasipugmailcom

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.