Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Maximize count of equal numbers in Array of numbers upto N by replacing pairs with their sum

  • Last Updated : 20 Apr, 2021

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:

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

Examples:

Attention reader! All those who say programming isn't for kids, just haven't met the right mentors yet. Join the  Demo Class for First Step to Coding Coursespecifically designed for students of class 8 to 12. 

The students will get to learn more about the world of programming in these free classes which will definitely help them in making a wise career choice in the future.

Input: arr[] = {1, 2, 3, 4} 
Output:
Explanation: 
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:
Explanation: 
{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

1+N=2+(N-1)=3+(N-2)=...

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

N=1+(N-1)=2+(N-2)=...
 

Therefore, the answer will always be 

\lceil \frac{N}{2} \rceil
 

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;
}

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

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

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

Javascript




<script>
 
// Javascript implementation of
// the above approach
 
// Function to count maximum number
// of array elements equal
function countEqual(n)
{
    return parseInt((n + 1) / 2);
}
 
// Driver Code
var arr = [ 1, 2, 3, 4, 5, 6 ];
var n = arr.length;
 
// Function Call
document.write( countEqual(n));
 
// This code is contributed by rrrtnx.
</script>
Output: 
3

Performance Analysis:

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



My Personal Notes arrow_drop_up
Recommended Articles
Page :