没有对和可被 K 整除的子集
给定一个整数数组,我们需要找到一个子集的最大大小,使得该子集的每一对之和不能被 K 整除。
例子 :
Input : arr[] = [3, 7, 2, 9, 1]
K = 3
Output : 3
Maximum size subset whose each pair sum
is not divisible by K is [3, 7, 1] because,
3+7 = 10,
3+1 = 4,
7+1 = 8 all are not divisible by 3.
It is not possible to get a subset of size
bigger than 3 with the above-mentioned property.
Input : arr[] = [3, 17, 12, 9, 11, 15]
K = 5
Output : 4
我们可以通过计算数组数与 K 的模来解决这个问题。如果两个数的和可以被 K 整除,那么如果其中一个数给出余数 i,另一个数将给出余数 (K – i)。首先,我们将给出特定余数的数字的频率存储在大小为 K 的频率数组中。然后我们循环所有余数 i 并包括 max(f[i], f[K – i])。为什么?一个没有对和可被 K 整除的子集必须包含余数为 f[i] 或余数为 f[K – i] 的元素。由于我们想最大化子集的大小,我们选择最多两个大小。
在下面的代码中,余数为 0 和余数为 K/2 的数组编号分别处理。如果我们包含超过 2 个余数为 0 的数字,那么它们的总和将可被 K 整除,因此我们在考虑中最多取 1 个数字,数组数字的余数为 K/2 的情况也是如此。
C++
// C++ program to get size of subset whose
// each pair sum is not divisible by K
#include
using namespace std;
// Returns maximum size of subset with no pair
// sum divisible by K
int subsetPairNotDivisibleByK(int arr[], int N,
int K)
{
// Array for storing frequency of modulo
// values
int f[K];
memset(f, 0, sizeof(f));
// Fill frequency array with values modulo K
for (int i = 0; i < N; i++)
f[arr[i] % K]++;
// if K is even, then update f[K/2]
if (K % 2 == 0)
f[K/2] = min(f[K/2], 1);
// Initialize result by minimum of 1 or
// count of numbers giving remainder 0
int res = min(f[0], 1);
// Choose maximum of count of numbers
// giving remainder i or K-i
for (int i = 1; i <= K/2; i++)
res += max(f[i], f[K-i]);
return res;
}
// Driver code to test above methods
int main()
{
int arr[] = {3, 7, 2, 9, 1};
int N = sizeof(arr) / sizeof(arr[0]);
int K = 3;
cout << subsetPairNotDivisibleByK(arr, N, K);
return 0;
}
Java
// Java program to get size of subset whose
// each pair sum is not divisible by K
import java.util.Arrays;
class Test {
// Returns maximum size of subset with no pair
// sum divisible by K
static int subsetPairNotDivisibleByK(int arr[],
int N, int K)
{
// Array for storing frequency of modulo
// values
int f[] = new int[K];
Arrays.fill(f, 0);
// Fill frequency array with values modulo K
for (int i = 0; i < N; i++)
f[arr[i] % K]++;
// if K is even, then update f[K/2]
if (K % 2 == 0)
f[K/2] = Math.min(f[K/2], 1);
// Initialize result by minimum of 1 or
// count of numbers giving remainder 0
int res = Math.min(f[0], 1);
// Choose maximum of count of numbers
// giving remainder i or K-i
for (int i = 1; i <= K/2; i++)
res += Math.max(f[i], f[K-i]);
return res;
}
// Driver method
public static void main(String[] args)
{
int arr[] = {3, 7, 2, 9, 1};
int N = arr.length;
int K = 3;
System.out.print(subsetPairNotDivisibleByK(
arr, N, K));
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python3 program to get size of
# subset whose each pair sum is
# not divisible by K
# Returns maximum size of subset
# with no pair sum divisible by K
def subsetPairNotDivisibleByK(arr, N, K):
# Array for storing frequency
# of modulo values
f = [0 for i in range(K)]
# Fill frequency array with
# values modulo K
for i in range(N):
f[arr[i] % K] += 1
# if K is even, then update f[K/2]
if (K % 2 == 0):
f[K//2] = min(f[K//2], 1)
# Initialize result by minimum of 1 or
# count of numbers giving remainder 0
res = min(f[0], 1)
# Choose maximum of count of numbers
# giving remainder i or K-i
for i in range(1,(K // 2) + 1):
res += max(f[i], f[K - i])
return res
# Driver Code
arr = [3, 7, 2, 9, 1]
N = len(arr)
K = 3
print(subsetPairNotDivisibleByK(arr, N, K))
# This code is contributed by Anant Agarwal.
C#
// C# program to get size of subset whose
// each pair sum is not divisible by K
using System;
class Test {
// Returns maximum size of subset
// with no pair sum divisible by K
static int subsetPairNotDivisibleByK(int []arr,
int N, int K)
{
// Array for storing
// frequency of modulo values
int []f = new int[K];
for(int i = 0; i < K; i++)
f[i] = 0;
// Fill frequency array with values modulo K
for (int i = 0; i < N; i++)
f[arr[i] % K]++;
// if K is even, then update f[K/2]
if (K % 2 == 0)
f[K/2] = Math.Min(f[K/2], 1);
// Initialize result by minimum of 1 or
// count of numbers giving remainder 0
int res = Math.Min(f[0], 1);
// Choose maximum of count of numbers
// giving remainder i or K-i
for (int i = 1; i <= K/2; i++)
res += Math.Max(f[i], f[K-i]);
return res;
}
// Driver method
public static void Main()
{
int []arr = {3, 7, 2, 9, 1};
int N = arr.Length;
int K = 3;
// Function calling
Console.Write(subsetPairNotDivisibleByK(arr, N, K));
}
}
// This code is contributed by nitin mittal.
PHP
Javascript
输出:
3