Divide array in two maximum equal length arrays of similar and dissimilar elements
Last Updated :
07 Dec, 2021
Given an array arr of natural numbers up to n, find the maximum size for which array arr can be divided into two equal-sized arrays, such that the first array contains all same elements while the second array contains all distinct elements.
Examples:
Input :n = 8, arr[] ={7, 3, 7, 1, 7, 7}
Output :
Maximum size is : 3
arr1[] ={7, 7, 7}
arr2[] ={1, 3, 7}
Explanation :
It is possible to construct two arrays of size 3.
The first array is [7, 7, 7] and the second array is [1, 3, 7].
Input : n = 7, arr[] ={1, 2, 1, 5, 1, 6, 7, 2}
Output :
Maximum size is : 3
arr1[] ={1, 1, 1}
arr2[] ={2, 5, 6}
Approach:
To solve the problem mentioned above the main idea is to use hashing to find the frequency of every element present in the array.
- Find the element with maximum frequency present in the array arr[] using hash vector v.
- Find the total unique elements present in array arr[].
- There are two cases for the element with maximum frequency: the maximum frequency element will go to the first array then the sizes of the array are at most diff1 – 1 and max1 correspondingly. Otherwise, at least one element of maximum frequency goes to the second array and the sizes are at most diff1 and max1 ? 1 correspondingly. Then find the max-size to which array can be splitted as max(min(diff1 ? 1, max1), min(diff1, max1 ? 1)).
- Find the first array of similar elements using max_size and element with maximum frequency max1.
- Find the second array of unique elements using max_size and hash vector v.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void Solve( int arr[], int size, int n)
{
vector< int > v(n + 1);
for ( int i = 0; i < size; i++)
v[arr[i]]++;
int max1 = (max_element(v.begin(), v.end())
- v.begin());
int diff1 = n + 1 - count(v.begin(), v.end(), 0);
int max_size = max(min(v[max1] - 1, diff1),
min(v[max1], diff1 - 1));
cout << "Maximum size is :" << max_size << "\n" ;
cout << "The First Array Is : \n" ;
for ( int i = 0; i < max_size; i++) {
cout << max1 << " " ;
v[max1] -= 1;
}
cout << "\n" ;
cout << "The Second Array Is : \n" ;
for ( int i = 0; i < (n + 1); i++) {
if (v[i] > 0) {
cout << i << " " ;
max_size--;
}
if (max_size < 1)
break ;
}
cout << "\n" ;
}
int main()
{
int n = 7;
int arr[] = { 1, 2, 1, 5, 1, 6, 7, 2 };
int size = sizeof (arr) / sizeof (arr[0]);
Solve(arr, size, n);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static void Solve( int arr[],
int size, int n)
{
int [] v = new int [n + 1 ];
for ( int i = 0 ; i < size; i++)
v[arr[i]]++;
int max1 = - 1 , mx = - 1 ;
for ( int i = 0 ; i < v.length; i++)
{
if (v[i] > mx)
{
mx = v[i];
max1 = i;
}
}
int cnt = 0 ;
for ( int i : v)
{
if (i == 0 )
++cnt;
}
int diff1 = n + 1 - cnt;
int max_size = Math.max(Math.min(v[max1] - 1 ,
diff1),
Math.min(v[max1],
diff1 - 1 ));
System.out.println( "Maximum size is: " +
max_size);
System.out.println( "First Array is" );
for ( int i = 0 ; i < max_size; i++)
{
System.out.print(max1 + " " );
v[max1] -= 1 ;
}
System.out.println();
System.out.println( "The Second Array Is :" );
for ( int i = 0 ; i < (n + 1 ); i++)
{
if (v[i] > 0 )
{
System.out.print(i + " " );
max_size--;
}
if (max_size < 1 )
break ;
}
System.out.println();
}
public static void main(String[] args)
{
int n = 7 ;
int arr[] = new int [] { 1 , 2 , 1 , 5 ,
1 , 6 , 7 , 2 };
int size = arr.length;
Solve(arr, size, n);
}
}
|
Python3
def Solve(arr, size, n):
v = [ 0 ] * (n + 1 );
for i in range (size):
v[arr[i]] + = 1
max1 = max ( set (arr), key = v.count)
diff1 = n + 1 - v.count( 0 )
max_size = max ( min (v[max1] - 1 , diff1),
min (v[max1], diff1 - 1 ))
print ( "Maximum size is :" , max_size)
print ( "The First Array Is : " )
for i in range (max_size):
print (max1, end = " " )
v[max1] - = 1
print ()
print ( "The Second Array Is : " )
for i in range (n + 1 ):
if (v[i] > 0 ):
print (i, end = " " )
max_size - = 1
if (max_size < 1 ):
break
print ()
if __name__ = = "__main__" :
n = 7
arr = [ 1 , 2 , 1 , 5 , 1 , 6 , 7 , 2 ]
size = len (arr)
Solve(arr, size, n)
|
C#
using System;
class GFG{
static void Solve( int []arr,
int size, int n)
{
int [] v = new int [n + 1];
for ( int i = 0; i < size; i++)
v[arr[i]]++;
int max1 = -1, mx = -1;
for ( int i = 0; i < v.Length; i++)
{
if (v[i] > mx)
{
mx = v[i];
max1 = i;
}
}
int cnt = 0;
foreach ( int i in v)
{
if (i == 0)
++cnt;
}
int diff1 = n + 1 - cnt;
int max_size = Math.Max(Math.Min(v[max1] - 1,
diff1),
Math.Min(v[max1],
diff1 - 1));
Console.Write( "Maximum size is :" +
max_size + "\n" );
Console.Write( "The First Array Is :\n" );
for ( int i = 0; i < max_size; i++)
{
Console.Write(max1 + " " );
v[max1] -= 1;
}
Console.Write( "\n" );
Console.Write( "The Second Array Is :\n" );
for ( int i = 0; i < (n + 1); i++)
{
if (v[i] > 0)
{
Console.Write(i + " " );
max_size--;
}
if (max_size < 1)
break ;
}
Console.Write( "\n" );
}
public static void Main( string [] args)
{
int n = 7;
int []arr = new int [] { 1, 2, 1, 5,
1, 6, 7, 2 };
int size = arr.Length;
Solve(arr, size, n);
}
}
|
Javascript
<script>
function Solve(arr, size, n)
{
let v = Array.from({length: n+1}, (_, i) => 0);
for (let i = 0; i < size; i++)
v[arr[i]]++;
let max1 = -1, mx = -1;
for (let i = 0; i < v.length; i++)
{
if (v[i] > mx)
{
mx = v[i];
max1 = i;
}
}
let cnt = 0;
for (let i in v)
{
if (i == 0)
++cnt;
}
let diff1 = n + 1 - cnt;
let max_size = Math.max(Math.min(v[max1] - 1,
diff1),
Math.min(v[max1],
diff1 - 1));
document.write( "Maximum size is: " +
max_size + "<br/>" );
document.write( "First Array is" + "<br/>" );
for (let i = 0; i < max_size; i++)
{
document.write(max1 + " " );
v[max1] -= 1;
}
document.write( "<br/>" );
document.write( "The Second Array Is :" + "<br/>" );
for (let i = 0; i < (n + 1); i++)
{
if (v[i] > 0)
{
document.write(i + " " );
max_size--;
}
if (max_size < 1)
break ;
}
document.write( "<br/>" );
}
let n = 7;
let arr = [1, 2, 1, 5,
1, 6, 7, 2];
let size = arr.length;
Solve(arr, size, n);
</script>
|
Output:
Maximum size is :3
The First Array Is :
1 1 1
The Second Array Is :
2 5 6
Time Complexity :O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...