给定一个由N 个整数和一个整数K组成的数组arr[] ,任务是打印通过获取数组元素形成的组的最小计数,使得每个组中相邻元素的总和可以被K整除。
例子:
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
方法:根据以下观察可以解决给定的问题:
- 可以观察到,应该形成一个组:
- 组应该只包含一个不能被K整除的元素。
- 群的所有元素都应该可以单独被K整除。
- 应该满足群的每一个相邻元素; X % K + Y % K = K ,其中X和Y是该组的两个相邻元素。
请按照以下步骤解决问题:
- 初始化一个 map
,比如mp,来存储arr[i] % K的计数。 - 初始化一个变量,比如ans为0,以存储组的计数。
- 遍历数组arr[]并增加mp中arr[i] % K的计数,如果arr[i] % K为0,则将1分配给ans 。
- 迭代范围[1, K / 2]并执行以下操作:
- 将mp[i]和mp[K – i]的最小值存储在一个变量中,比如C1。
- 将mp[i]和mp[K – i]的最大值存储在一个变量中,比如C2。
- 如果C1为0,则将ans增加C2 。
- 否则,如果C1等于C1或C1 + 1,则将ans增加1 。
- 否则,将ans增加C2 – C1 – 1。
- 最后,完成上述步骤后,打印ans 中得到的答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the minimum number
// of groups
int findMinGroups(int arr[], int N, int K)
{
// Stores the count of elements
unordered_map mp;
// Stores the count of groups
int ans = 0;
// Traverse the array arr[]
for (int i = 0; i < N; i++) {
// Update arr[i]
arr[i] = arr[i] % K;
// If arr[i] is 0
if (arr[i] == 0) {
// Update ans
ans = 1;
}
else {
// Increment mp[arr[i]] by 1
mp[arr[i]]++;
}
}
// Iterarte over the range [1, K / 2]
for (int i = 1; i <= K / 2; i++) {
// Stores the minimum of count of i
// and K - i
int c1 = min(mp[K - i], mp[i]);
// Stores the maximum of count of i
// and K - i
int c2 = max(mp[K - i], mp[i]);
// If c1 is 0
if (c1 == 0) {
// Increment ans by c2
ans += c2;
}
// Otherwise if c2 is equal to c1 + 1
// or c1
else if (c2 == c1 + 1 || c1 == c2) {
// Increment ans by 1
ans++;
}
// Otherwise
else {
// Increment ans by c2 - c1 - 1
ans += (c2 - c1 - 1);
}
}
// Return the ans
return ans;
}
// Driver Code
int main()
{
// Input
int arr[] = { 1, 1, 4, 4, 8, 6, 7 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = 8;
// Function Call
cout << findMinGroups(arr, N, K);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG
{
// Function to count the minimum number
// of groups
public static int findMinGroups(int arr[], int N, int K)
{
// Stores the count of elements
HashMap mp = new HashMap<>();
// Stores the count of groups
int ans = 0;
// Traverse the array arr[]
for (int i = 0; i < N; i++) {
// Update arr[i]
arr[i] = arr[i] % K;
// If arr[i] is 0
if (arr[i] == 0) {
// Update ans
ans = 1;
}
else {
// Increment mp[arr[i]] by 1
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);
}
}
}
}
// Iterarte over the range [1, K / 2]
for (int i = 1; i <= K / 2; i++) {
// Stores the minimum of count of i
// and K - 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);
// Stores the maximum of count of i
// and K - i
int c2 = Math.max(a, b);
// If c1 is 0
if (c1 == 0) {
// Increment ans by c2
ans += c2;
}
// Otherwise if c2 is equal to c1 + 1
// or c1
else if (c2 == c1 + 1 || c1 == c2) {
// Increment ans by 1
ans++;
}
// Otherwise
else {
// Increment ans by c2 - c1 - 1
ans += (c2 - c1 - 1);
}
}
// Return the ans
return ans;
}
public static void main (String[] args) {
// Input
int arr[] = { 1, 1, 4, 4, 8, 6, 7 };
int N = 7;
int K = 8;
// Function Call
System.out.println(findMinGroups(arr, N, K));
}
}
// This code is contributed by Manu Pathria
Python3
# Python3 program for the above approach
# Function to count the minimum number
# of groups
def findMinGroups(arr, N, K):
# Stores the count of elements
mp = {}
# Stores the count of groups
ans = 0
# Traverse the array arr[]
for i in range(N):
# Update arr[i]
arr[i] = arr[i] % K
# If arr[i] is 0
if (arr[i] == 0):
# Update ans
ans = 1
else:
mp[arr[i]] = mp.get(arr[i], 0) + 1
# Iterarte over the range [1, K / 2]
for i in range(1, K // 2):
# Stores the minimum of count of i
# and K - i
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)
# The maximum of count of i
# K - i
c2 = max(x, y)
# If c1 is 0
if (c1 == 0):
# Increment ans by c2
ans += c2
# Otherwise if c2 is equal to c1 + 1
# or c1
elif (c2 == c1 + 1 or c1 == c2):
# Increment ans by 1
ans += 1
# Otherwise
else:
# Increment ans by c2 - c1 - 1
ans += (c2 - c1 - 1)
# Return the ans
return ans + 1
# Driver code
if __name__ == '__main__':
# Input
arr = [ 1, 1, 4, 4, 8, 6, 7 ]
N = len(arr)
K = 8
# Function Call
print(findMinGroups(arr, N, K))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG{
// Function to count the minimum number
// of groups
static int findMinGroups(int[] arr, int N, int K)
{
// Stores the count of elements
Dictionary mp = new Dictionary();
// Stores the count of groups
int ans = 0;
// Traverse the array arr[]
for (int i = 0; i < N; i++) {
// Update arr[i]
arr[i] = arr[i] % K;
// If arr[i] is 0
if (arr[i] == 0) {
// Update ans
ans = 1;
}
else {
// Increment mp[arr[i]] by 1
if (!mp.ContainsKey(arr[i])) {
mp.Add(arr[i], 1);
}
else {
int ct = mp[arr[i]];
if(ct!=0)
{
ct++;
mp[arr[i]]= ct;
}
}
}
}
// Iterarte over the range [1, K / 2]
for (int i = 1; i <= K / 2; i++) {
// Stores the minimum of count of i
// and K - 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);
// Stores the maximum of count of i
// and K - i
int c2 = Math.Max(a, b);
// If c1 is 0
if (c1 == 0) {
// Increment ans by c2
ans += c2;
}
// Otherwise if c2 is equal to c1 + 1
// or c1
else if (c2 == c1 + 1 || c1 == c2) {
// Increment ans by 1
ans++;
}
// Otherwise
else {
// Increment ans by c2 - c1 - 1
ans += (c2 - c1 - 1);
}
}
// Return the ans
return ans;
}
static public void Main ()
{
// InAdd
int[] arr = { 1, 1, 4, 4, 8, 6, 7 };
int N = 7;
int K = 8;
// Function Call
Console.Write(findMinGroups(arr, N, K));
}
}
// This code is contributed by shubhamsingh10
Javascript
4
时间复杂度: O(N)
辅助空间: O(K)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。