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 below operations:
Examples:
Input:_ arr[] = {1, 2, 3, 4} _
Output:_ 2 _
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:_ 3 _
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:
Therefore, the answer will always be
Below is the implementation of the above approach:
// 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;
}
Output:
3
Performance Analysis:
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.
#arrays #mathematical #school programming #natural numbers