Given an array arr[] consisting of N distinct integers, the task is to find the minimum number of times the array needs to be split into two subsets such that elements of each pair are present into two different subsets at least once.
Examples:
Input: arr[] = { 3, 4, 2, 1, 5 }
Output: 3
Explanation:
Possible pairs are { (1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5) }
Splitting the array into { 1, 2 } and { 3, 4, 5 }
Elements of each of the pairs { (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5) } are present into two different subsets.
Splitting the array into { 1, 3 } and { 2, 4, 5 }
Elements of each of the pairs { (1, 2), (1, 4), (1, 5), (2, 3), (3, 4), (3, 5) } are present into two different subsets.
Splitting the array into { 1, 3, 4 } and { 2, 5 }
Elements of each of the pairs { (1, 2), (1, 5), (2, 3), (3, 5), (2, 4), (4, 5) } are present into two different subsets.
Since elements of each pair of the array is present in two different subsets at least once, the required output is 3.
Input: arr[] = { 2, 1, 3 }
Output: 2
Approach: The idea is to always split the array into two subsets of size floor(N / 2) and ceil(N / 2). Before each partition just swap the value of arr[i] with arr[N / 2 + i]. Follow the steps given below to solve the problem:
Below is the C++ implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int MinimumNoOfWays( int arr[], int n)
{
int mini_no_of_ways;
if (n % 2 == 0) {
mini_no_of_ways = n / 2;
}
else {
mini_no_of_ways = n / 2 + 1;
}
return mini_no_of_ways;
}
int main()
{
int arr[] = { 3, 4, 2, 1, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << MinimumNoOfWays(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int MinimumNoOfWays( int arr[], int n)
{
int mini_no_of_ways;
if (n % 2 == 0 ) {
mini_no_of_ways = n / 2 ;
}
else {
mini_no_of_ways = n / 2 + 1 ;
}
return mini_no_of_ways;
}
public static void main(String[] args)
{
int arr[] = { 3 , 4 , 2 , 1 , 5 };
int N = arr.length;
System.out.print(MinimumNoOfWays(arr, N));
}
}
|
Python3
def MinimumNoOfWays(arr, n):
min_no_of_ways = 0
if (n % 2 = = 0 ):
mini_no_of_ways = n / / 2
else :
mini_no_of_ways = n / / 2 + 1
return mini_no_of_ways
if __name__ = = '__main__' :
arr = [ 3 , 4 , 1 , 2 , 5 ]
n = len (arr)
print (MinimumNoOfWays(arr, n))
|
C#
using System;
class GFG
{
static int MinimumNoOfWays( int []arr, int n)
{
int mini_no_of_ways;
if (n % 2 == 0)
{
mini_no_of_ways = n / 2;
}
else
{
mini_no_of_ways = n / 2 + 1;
}
return mini_no_of_ways;
}
public static void Main( string [] args)
{
int [] arr = { 3, 4, 2, 1, 5 };
int N = arr.Length;
Console.WriteLine(MinimumNoOfWays(arr, N));
}
}
|
Javascript
<script>
function MinimumNoOfWays(arr , n) {
var mini_no_of_ways;
if (n % 2 == 0) {
mini_no_of_ways = n / 2;
} else {
mini_no_of_ways = n / 2 + 1;
}
return parseInt(mini_no_of_ways);
}
var arr = [ 3, 4, 2, 1, 5 ];
var N = arr.length;
document.write(MinimumNoOfWays(arr, N));
</script>
|
Time Complexity: O(1)
Space Complexity: O(1)