Minimum count of groups such that sum of adjacent elements is divisible by K in each group
Last Updated :
28 Jul, 2021
Given an array arr[] consisting of N integers, and an integer K, the task is to print the minimum count of groups formed by taking elements of the array such that the sum of adjacent elements is divisible by K in each group.
Examples:
Input: arr[] = {2, 6, 5, 8, 2, 9}, K = 2
Output: 2
The array can be split into two groups {2, 6, 2, 8} and {5, 9}.
The sum of adjacent elements of each group is divisible by K.
Thus, the minimum number of groups that can be formed is 2.
Input: arr[] = {1, 1, 4, 4, 8, 6, 7}, K = 8
Output: 4
Explanation:
The array can be split into 4 groups: {1, 7, 1}, {4, 4}, {8}, and {6}.
The sum of adjacent elements of each group is divisible by K.
Thus, the minimum number of groups that can be formed is 4.
Input: arr[] = {144}, K = 5
Output: 1
Approach: The given problem can be solved based on the following observations:
- It can be observed that a group should be formed as:
- Groups should consist of only one element which is not divisible by K.
- All elements of the groups should be individually divisible by K.
- Every adjacent element of the group should be satisfied; X % K + Y % K = K, where X and Y are two adjacent elements of the group.
Follow the steps below to solve the problem:
- Initialize a map<int, int>, say mp, to store the count of arr[i] % K.
- Initialize a variable, say ans as 0, to store the count of groups.
- Traverse the array arr[] and increment the count of arr[i] % K in the mp and if arr[i] % K is 0 then assign 1 to ans.
- Iterate over the range [1, K / 2] and perform the following operations:
- Store the minimum of mp[i] and mp[K – i] in a variable, say C1.
- Store the maximum of mp[i] and mp[K – i] in a variable, say C2.
- If C1 is 0 then increment ans by C2.
- Otherwise, if C1 is either equal to C1 or C1 + 1, then increment ans by 1.
- Otherwise, increment ans by C2 – C1 – 1.
- Finally, after completing the above steps, print the answer obtained in ans.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findMinGroups( int arr[], int N, int K)
{
unordered_map< int , int > mp;
int ans = 0;
for ( int i = 0; i < N; i++) {
arr[i] = arr[i] % K;
if (arr[i] == 0) {
ans = 1;
}
else {
mp[arr[i]]++;
}
}
for ( int i = 1; i <= K / 2; i++) {
int c1 = min(mp[K - i], mp[i]);
int c2 = max(mp[K - i], mp[i]);
if (c1 == 0) {
ans += c2;
}
else if (c2 == c1 + 1 || c1 == c2) {
ans++;
}
else {
ans += (c2 - c1 - 1);
}
}
return ans;
}
int main()
{
int arr[] = { 1, 1, 4, 4, 8, 6, 7 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 8;
cout << findMinGroups(arr, N, K);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
public static int findMinGroups( int arr[], int N, int K)
{
HashMap<Integer, Integer> mp = new HashMap<>();
int ans = 0 ;
for ( int i = 0 ; i < N; i++) {
arr[i] = arr[i] % K;
if (arr[i] == 0 ) {
ans = 1 ;
}
else {
if (!mp.containsKey(arr[i])) {
mp.put(arr[i], 1 );
}
else {
Integer ct = mp.get(arr[i]);
if (ct!= null )
{
ct++;
mp.put(arr[i], ct);
}
}
}
}
for ( int i = 1 ; i <= K / 2 ; i++) {
int a= 0 ,b= 0 ;
if (mp.containsKey(K-i)){
a=mp.get(K-i);
}
if (mp.containsKey(i)){
b=mp.get(i);
}
int c1 = Math.min(a, b);
int c2 = Math.max(a, b);
if (c1 == 0 ) {
ans += c2;
}
else if (c2 == c1 + 1 || c1 == c2) {
ans++;
}
else {
ans += (c2 - c1 - 1 );
}
}
return ans;
}
public static void main (String[] args) {
int arr[] = { 1 , 1 , 4 , 4 , 8 , 6 , 7 };
int N = 7 ;
int K = 8 ;
System.out.println(findMinGroups(arr, N, K));
}
}
|
Python3
def findMinGroups(arr, N, K):
mp = {}
ans = 0
for i in range (N):
arr[i] = arr[i] % K
if (arr[i] = = 0 ):
ans = 1
else :
mp[arr[i]] = mp.get(arr[i], 0 ) + 1
for i in range ( 1 , K / / 2 ):
x, y = ( 0 if (K - i) not in mp else mp[K - i],
0 if i not in mp else mp[K - i])
c1 = min (x, y)
c2 = max (x, y)
if (c1 = = 0 ):
ans + = c2
elif (c2 = = c1 + 1 or c1 = = c2):
ans + = 1
else :
ans + = (c2 - c1 - 1 )
return ans + 1
if __name__ = = '__main__' :
arr = [ 1 , 1 , 4 , 4 , 8 , 6 , 7 ]
N = len (arr)
K = 8
print (findMinGroups(arr, N, K))
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static int findMinGroups( int [] arr, int N, int K)
{
Dictionary< int , int > mp = new Dictionary< int , int >();
int ans = 0;
for ( int i = 0; i < N; i++) {
arr[i] = arr[i] % K;
if (arr[i] == 0) {
ans = 1;
}
else {
if (!mp.ContainsKey(arr[i])) {
mp.Add(arr[i], 1);
}
else {
int ct = mp[arr[i]];
if (ct!=0)
{
ct++;
mp[arr[i]]= ct;
}
}
}
}
for ( int i = 1; i <= K / 2; i++) {
int a=0,b=0;
if (mp.ContainsKey(K-i)){
a=mp[K-i];
}
if (mp.ContainsKey(i)){
b=mp[i];
}
int c1 = Math.Min(a, b);
int c2 = Math.Max(a, b);
if (c1 == 0) {
ans += c2;
}
else if (c2 == c1 + 1 || c1 == c2) {
ans++;
}
else {
ans += (c2 - c1 - 1);
}
}
return ans;
}
static public void Main ()
{
int [] arr = { 1, 1, 4, 4, 8, 6, 7 };
int N = 7;
int K = 8;
Console.Write(findMinGroups(arr, N, K));
}
}
|
Javascript
<script>
function findMinGroups(arr, N, K)
{
let mp = new Map();
let ans = 0;
for (let i = 0; i < N; i++) {
arr[i] = arr[i] % K;
if (arr[i] == 0) {
ans = 1;
}
else {
if (!mp.has(arr[i])) {
mp.set(arr[i], 1);
}
else {
let ct = mp.get(arr[i]);
if (ct != null ) {
ct++;
mp.set(arr[i], ct);
}
}
}
}
for (let i = 1; i <= K / 2; i++) {
let a = 0, b = 0;
if (mp.has(K - i)) {
a = mp.get(K - i);
}
if (mp.has(i)) {
b = mp.get(i);
}
let c1 = Math.min(a, b);
let c2 = Math.max(a, b);
if (c1 == 0) {
ans += c2;
}
else if (c2 == c1 + 1 || c1 == c2) {
ans++;
}
else {
ans += (c2 - c1 - 1);
}
}
return ans;
}
let arr = [1, 1, 4, 4, 8, 6, 7];
let N = 7;
let K = 8;
document.write(findMinGroups(arr, N, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(K)
Share your thoughts in the comments
Please Login to comment...