Minimum groups to split Array such that their each pair value difference and position difference are same
Last Updated :
15 Feb, 2023
Given an array arr[] consisting of N integers, the task is to split the array into the minimum number of disjoint groups, such that differences between any pair of the elements in a group are equal to the difference between their positions in that group.
Examples:
Input: arr[] = {30, 32, 44, 31, 45, 32, 31, 33}
Output: 3
Explanation:
The one possible way to split the array is:
- First group: {30, 31, 32, 33}
- Second group: {31, 32}
- Third group: {44, 45}
Therefore, the number of groups is 3, which is the minimum number of groups in which the array can be split satisfying the conditions.
Input: arr[] = {1, 5, 3, 1, 7, 7, 9}
Output: 7
Naive Approach: The simplest approach is, split the array into K subsets for every integer K less than or equal to N, and then check if all the subsets of the array satisfy the given conditions or not. If found to be true, then print the minimum size of the partition possible.
Time Complexity: O(N(N + 2))
Auxiliary Space: O(N)
Efficient Approach: The above approach can be optimized based on the observation that the elements in each group must be sorted and the difference between adjacent elements should be 1. Follow the steps below to solve the problem:
- Sort the given array in ascending order.
- Initialize a Map, say mp to store the frequency of array elements.
- Initialize a variable, say count as 0 to store the count of the minimum number of disjoint groups formed.
- Traverse the given array arr[], and increment the frequency of each element in the map mp.
- Traverse the given array arr[], using the variable i, and perform the following steps:
- If the count of arr[i] in map mp is at least 1, and the count of (arr[i] – 1) in the map mp then:
- Increment the value of count by 1.
- Iterate until the count of arr[i] in mp is greater than 0 and in each iteration, decrement mp[arr[i]] by 1 and increment arr[i] by 1.
- Finally, after completing the above steps, print the value of the count as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int numberOfSplit( int arr[], int N)
{
sort(arr, arr + N);
unordered_map< int , int > mp;
for ( int i = 0; i < N; i++)
mp[arr[i]]++;
int count = 0;
for ( int i = 0; i < N; i++) {
if (mp[arr[i]] > 0 && mp[arr[i] - 1] == 0) {
count++;
while (mp[arr[i]] != 0) {
mp[arr[i]]--;
arr[i]++;
}
}
}
return count;
}
int main()
{
int arr[] = { 30, 32, 44, 31, 45, 32, 31, 33 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << numberOfSplit(arr, N);
return 0;
}
|
Python3
def numberOfSplit(arr, N):
arr.sort()
mp = {}
for i in range (N):
if (arr[i] in mp):
mp[arr[i]] = mp[arr[i]] + 1
else :
mp[arr[i]] = 1
count = 0
for i in range (N):
if (arr[i] in mp and mp[arr[i]]> 0 and ((arr[i] - 1 ) not in mp or ((arr[i] - 1 ) in mp and mp[arr[i] - 1 ] = = 0 ))):
count + = 1
while arr[i] in mp and mp[arr[i]] > 0 :
mp[arr[i]] = mp[arr[i]] - 1
arr[i] + = 1
return count
arr = [ 30 , 32 , 44 , 31 , 45 , 32 , 31 , 33 ]
N = len (arr)
print (numberOfSplit(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int numberOfSplit( int []arr, int N)
{
Array.Sort(arr);
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
for ( int i = 0; i < N; i++)
{
if (mp.ContainsKey(arr[i]))
mp[arr[i]] += 1;
else
mp.Add(arr[i], 0);
}
int count = 2;
for ( int i = 0; i < N; i++)
{
if (mp[arr[i]] > 0 && mp[arr[i] - 1] == 0)
{
count++;
while (mp[arr[i]] != 0)
{
mp[arr[i]]--;
arr[i]++;
}
}
}
return count;
}
public static void Main()
{
int []arr = { 30, 32, 44, 31,
45, 32, 31, 33 };
int N = arr.Length;
Console.Write(numberOfSplit(arr, N));
}
}
|
Java
import java.util.*;
class GFG {
static int numberOfSplit( int [] arr, int N) {
Arrays.sort(arr);
Map<Integer, Integer> mp = new HashMap<>();
for ( int i = 0 ; i < N; i++) {
if (mp.containsKey(arr[i])) {
mp.put(arr[i], mp.get(arr[i]) + 1 );
} else {
mp.put(arr[i], 0 );
}
}
int count = 2 ;
for ( int i = 0 ; i < N; i++) {
if (mp.get(arr[i]) > 0 && mp.get(arr[i] - 1 ) == 0 ) {
count++;
while (mp.get(arr[i]) != 0 ) {
mp.put(arr[i], mp.get(arr[i]) - 1 );
arr[i]++;
}
}
}
return count;
}
public static void main(String[] args) {
int [] arr = { 30 , 32 , 44 , 31 , 45 , 32 , 31 , 33 };
int N = arr.length;
System.out.println(numberOfSplit(arr, N));
}
}
|
Javascript
<script>
function numberOfSplit(arr, N)
{
arr.sort();
var mp = new Map();
for ( var i = 0; i < N; i++)
{
if (mp.has(arr[i]))
mp.set(arr[i], mp.get(arr[i])+1)
else
mp.set(arr[i], 1)
}
var count = 0;
for ( var i = 0; i < N; i++) {
if (mp.has(arr[i]) && mp.get(arr[i])>0 && (!mp.has(arr[i] - 1) || (mp.has(arr[i]-1) && mp.get(arr[i]-1)==0))) {
count++;
while (mp.get(arr[i]) > 0) {
mp.set(arr[i], mp.get(arr[i])-1);
arr[i]++;
}
}
}
return count;
}
var arr = [30, 32, 44, 31, 45, 32, 31, 33];
var N = arr.length;
document.write(numberOfSplit(arr, N));
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...