Maximize distinct elements of Array by combining two elements or splitting an element
Last Updated :
13 Apr, 2023
Given an array arr[] of length N, the task is to maximize the number of distinct elements in the array by performing either of the following operations, any number of times:
- For an index i(0 ≤ i < N), replace arr[i] with a and b such that arr[i] = a + b.
- For two indices i (0 ≤ i < N) and n (0 ≤ n < N), Replace arr[n] with (arr[i] + arr[n]). Pop arr[i] from the array.
Examples:
Input: arr[] = {1, 4, 2, 8}, N = 4
Output: 5
Explanation: arr[3] can be split into [3, 5] to form arr [] = {1, 4, 2, 3, 5}.
There is no other way to split this into more elements.
Input: arr[] = {1, 1, 4, 3}, N = 4
Output: 3
Explanation: No operations can be performed to increase the number of distinct elements.
Approach: The problem can be based on the following observation:
Using the second operation, the entire arr[] can be reduced to 1 element, such that arr[0] = sum(arr[]). Now, the array sum can be partitioned into maximum number of unique parts get maximum unique elements.
Follow the below steps to implement the observation:
- Iterate over the array and find the sum of array elements (say sum).
- Now to get the maximum unique partitions of sum, it is optimal to assign as low a value as possible to each part.
- So, loop from i = 1 as long as sum > 0:
- Subtract i from sum and then increment i by 1.
- The total number of unique elements is i – 1, as there is an extra incrementation at the last iteration of the loop.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int maxUniqueElems( int * Arr, int L)
{
int sums = 0;
for ( int j = 0; j < L; j++)
sums += Arr[j];
int i = 1;
while (sums > 0) {
sums -= i;
i++;
}
return i - 1;
}
int main()
{
int arr[] = { 1, 4, 2, 8 };
int N = 4;
cout << maxUniqueElems(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static int maxUniqueElems( int []Arr, int L)
{
int sums = 0 ;
for ( int j = 0 ; j < L; j++)
sums += Arr[j];
int i = 1 ;
while (sums > 0 ) {
sums -= i;
i++;
}
return i - 1 ;
}
public static void main(String []args)
{
int arr[] = new int []{ 1 , 4 , 2 , 8 };
int N = 4 ;
System.out.println(maxUniqueElems(arr, N));
}
}
|
Python3
def maxUniqueElems(Arr, L):
sums = 0
for j in range ( 0 , L):
sums + = Arr[j]
i = 1
while (sums > 0 ):
sums - = i
i + = 1
return i - 1
if __name__ = = "__main__" :
arr = [ 1 , 4 , 2 , 8 ]
N = 4
print (maxUniqueElems(arr, N))
|
Javascript
<script>
function maxUniqueElems(Arr, L){
let sums = 0
for (let j = 0; j < L; j++)
sums += Arr[j]
let i = 1
while (sums > 0){
sums -= i
i += 1
}
return i - 1
}
let arr = [1, 4, 2, 8]
let N = 4
document.write(maxUniqueElems(arr, N), "</br>" )
</script>
|
C#
using System;
class GFG {
static int maxUniqueElems( int [] Arr, int L)
{
int sums = 0;
for ( int j = 0; j < L; j++)
sums += Arr[j];
int i = 1;
while (sums > 0) {
sums -= i;
i++;
}
return i - 1;
}
public static void Main()
{
int [] arr = { 1, 4, 2, 8 };
int N = 4;
Console.WriteLine(maxUniqueElems(arr, N));
}
}
|
Time Complexity: O(max(N, sqrt(S))) where S is the sum of array
Auxiliary Space: O(1)
Another Approach:
- First, the necessary header file is included using the #include preprocessor directive. The bits/stdc++.h header file includes all standard library header files, making it easier to write code.
- The maxUniqueElems() function takes an integer array (Arr) and its length (L) as input arguments.
- The variable “sums” is initialized to 0.
- The for loop is used to calculate the sum of all elements of the input array. It iterates over each element of the array using the loop variable “j”, and adds the value of the element to the “sums” variable.
- The next step is to calculate the total number of distinct elements in the input array. This is done using the formula: i = (sqrt(8*sums + 1) – 1) / 2 The formula is derived from the quadratic equation: n(n+1)/2 = sums, where n is the number of distinct elements. Solving the equation for n gives the above formula.
- Finally, the maxUniqueElems() function returns the value of “i”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxUniqueElems( int * Arr, int L)
{
int sums = 0;
for ( int j = 0; j < L; j++)
sums += Arr[j];
int i = ( sqrt (8*sums + 1) - 1) / 2;
return i;
}
int main()
{
int arr[] = { 1, 4, 2, 8 };
int N = 4;
cout << maxUniqueElems(arr, N);
return 0;
}
|
Java
import java.util.*;
public class Main {
static int maxUniqueElems( int [] Arr, int L) {
int sums = 0 ;
for ( int j = 0 ; j < L; j++)
sums += Arr[j];
int i = ( int ) ((Math.sqrt( 8 * sums + 1 ) - 1 ) / 2 );
return i;
}
public static void main(String[] args) {
int [] arr = { 1 , 4 , 2 , 8 };
int N = 4 ;
System.out.println(maxUniqueElems(arr, N));
}
}
|
Python3
import math
def maxUniqueElems(Arr, L):
sums = 0
for j in range (L):
sums + = Arr[j]
i = ( int (math.sqrt( 8 * sums + 1 )) - 1 ) / / 2
return i
if __name__ = = '__main__' :
arr = [ 1 , 4 , 2 , 8 ]
N = 4
print (maxUniqueElems(arr, N))
|
C#
using System;
public class GFG {
static int maxUniqueElems( int [] Arr, int L) {
int sums = 0;
for ( int j = 0; j < L; j++)
sums += Arr[j];
int i = ( int ) ((Math.Sqrt(8 * sums + 1) - 1) / 2);
return i;
}
public static void Main() {
int [] arr = {1, 4, 2, 8};
int N = 4;
Console.Write(maxUniqueElems(arr, N));
}
}
|
Javascript
function maxUniqueElems(arr) {
let sums = 0;
for (let j = 0; j < arr.length; j++) {
sums += arr[j];
}
let i = Math.floor((Math.sqrt(8 * sums + 1) - 1) / 2);
return i;
}
const arr = [1, 4, 2, 8];
console.log(maxUniqueElems(arr));
|
- In the main() function, an integer array arr[] is declared and initialized with some values.
- The length of the array is stored in the integer variable N.
- The maxUniqueElems() function is called with the array arr[] and its length N as input arguments.
- The output of the maxUniqueElems() function is printed to the console using the cout statement.
- The program ends with a return 0 statement.
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...