Given an array **arr[]** containing natural numbers from **1** to **N**, the task is to find the maximum number of elements that can be made equal after the below operations:

- Remove any pair of elements from the array and insert their sum to an array.
- Repeat the above operation any numbers of times to maximize the count of equal elements.

**Examples:**

Input:arr[] = {1, 2, 3, 4}Output:2Explanation:

We can perform following operations:

{1, 2, 3, 4} -> {3, 3, 4} -> 2 elements are equal

Input:arr[] = {1 2 3 4 5 6}Output:3Explanation:

{1, 2, 3, 4, 5, 6} -> {7, 2, 3, 4, 5} -> {7, 7, 3, 4} -> {7, 7, 37} -> 3 elements are equal

**Approach: **The key observation in the problem is that:

**If N is even**, we can make a maximum count of equal elements by

**If N is odd**, we can make the maximum count of equal elements by

Therefore, the answer will always be

Below is the implementation of the above approach:

## C++

`// C++ implementation of` `// the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to count maximum number` `// of array elements equal` `int` `countEqual(` `int` `n)` `{` ` ` `return` `(n + 1) / 2;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `arr[] = { 1, 2, 3, 4, 5, 6 };` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `// Function Call` ` ` `cout << countEqual(n);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of` `// the above approach` `import` `java.io.*;` `class` `GFG{` `// Function to count maximum number` `// of array elements equal` `static` `int` `countEqual(` `int` `n)` `{` ` ` `return` `(n + ` `1` `) / ` `2` `;` `}` `// Driver Code` `public` `static` `void` `main (String[] args)` `{` ` ` `int` `arr[] = { ` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `, ` `6` `};` ` ` `int` `n = arr.length;` ` ` `// Function call` ` ` `System.out.println(countEqual(n));` `}` `}` `// This code is contributed by AnkitRai01` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of ` `# the above approach ` `# Function to count maximum number ` `# of array elements equal ` `def` `countEqual(n):` ` ` `return` `(n ` `+` `1` `) ` `/` `/` `2` `# Driver Code ` `lst ` `=` `[ ` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `, ` `6` `]` `n ` `=` `len` `(lst) ` `# Function call` `print` `(countEqual(n))` `# This code is contributed by vishu2908` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of` `// the above approach` `using` `System;` `class` `GFG{` `// Function to count maximum number` `// of array elements equal` `static` `int` `countEqual(` `int` `n)` `{` ` ` `return` `(n + 1) / 2;` `}` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` ` ` `int` `[]arr = {1, 2, 3, 4, 5, 6};` ` ` `int` `n = arr.Length;` ` ` `// Function call` ` ` `Console.WriteLine(countEqual(n));` `}` `}` `// This code is contributed by Rajput-Ji` |

*chevron_right*

*filter_none*

**Output:**

3

**Performance Analysis:**

**Time Complexity:**O(1)**Auxiliary Space:**O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Maximize array sum by replacing equal adjacent pairs by their sum and X respectively
- Maximize sum of squares of array elements possible by replacing pairs with their Bitwise AND and Bitwise OR
- Maximize modulus by replacing adjacent pairs with their modulus for any permutation of given Array
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Minimize array length by repeatedly replacing pairs of unequal adjacent array elements by their sum
- Count of pairs upto N such whose LCM is not equal to their product for Q queries
- Smallest array that can be obtained by replacing adjacent pairs with their products
- Count pairs from 1 to N such that their Sum is divisible by their XOR
- Minimize swaps required to maximize the count of elements replacing a greater element in an Array
- Sum of product of all integers upto N with their count of divisors
- Count of pairs from Array with sum equal to twice their bitwise AND
- Count pairs from an array having product of their sum and difference equal to 1
- Count pairs from an array having product of their sum and difference equal to 0
- Array value by repeatedly replacing max 2 elements with their absolute difference
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Maximize length of increasing subsequence possible by replacing array element by nearest primes
- Count the pairs in an array such that the difference between them and their indices is equal
- Last element remaining by deleting two largest elements and replacing by their absolute difference if they are unequal
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Maximize array elements upto given number

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.