检查是否有任何有效的序列可以被 M 整除


序列是一组对象,在本例中,它是一组整数。任务是使用元素内的加法和减法运算符来查找序列是否可以被 M 整除。

问题陈述

给定一个整数 M 和一个整数数组。仅使用元素之间的加法和减法,检查是否存在一个有效序列,其解可以被 M 整除。

示例 1

Input: M = 2, arr = {1, 2, 5}
Output: TRUE

说明 − 对于给定的数组,存在一个有效的序列 {1 + 2 + 5} = {8},它可以被 2 整除。

示例 2

Input: M = 4, arr = {1, 2}
Output: FALSE

说明 − 对于给定的数组,不存在任何序列,其解可以被 4 整除。

方法 1:暴力法

解决这个问题的一个简单方法是使用递归函数查找数组的所有可能序列,然后检查是否有任何序列可以被 M 整除。

伪代码

procedure divisible (M, arr[], index, sum, n)
   if index == n
      if sum is a multiple of M
         ans = TRUE
      end if
      ans = false
   end if
   divisible(M, arr, index + 1, sum + arr[index], n) or divisible(M, arr, index + 1, sum - arr[index], n)
end procedure

示例:C++ 实现

在下面的程序中,我们使用递归方法查找所有有效序列,然后检查是否有任何有效序列可以被 M 整除。

#include <bits/stdc++.h>
using namespace std;

// Recusive function to find if a valid sequence is divisible by M or not
bool divisible(int M, int arr[], int index, int sum, int n){

   // Cheking the divisiblilty by M when the array ends
   if (index == n)  {
      if (sum % M == 0){
         return true;
      }
      return false;
   }
   
   // If either of addition or subtraction is true, return true
   return divisible(M, arr, index + 1, sum + arr[index], n) || divisible(M, arr, index + 1, sum - arr[index], n);
}
int main(){
   int M = 4, arr[2] = {1, 5};
   if (divisible(M, arr, 0, 0, 2)){
      cout << "TRUE";
   }
   else{
      cout << " FALSE";
   }
   return 0;
}

输出

TRUE

时间复杂度 − O(2^n),因为使用了递归。

空间复杂度 − O(n),因为使用了递归栈空间。

方法 2:回溯法

这种方法类似于之前的暴力递归方法,不同之处在于,使用回溯法,我们可以回溯搜索空间,避免走上已知没有有效序列可以被 M 整除的路径。

伪代码

procedure divisible (M, arr[], index, sum, n)
   if index == n
      if sum is a multiple of M
         ans = TRUE
      end if
      ans = false
   end if
   if divisible(M, arr, index + 1, sum + arr[index], n)
      ans = true
   end if
   if divisible(M, arr, index + 1, sum - arr[index], n)
      ans = true
   end if
   ans = false
end procedure

示例:C++ 实现

在下面的程序中,我们使用回溯法修剪搜索空间,以找到问题的解。

#include <bits/stdc++.h>
using namespace std;

// Function to find if a valid sequence is divisible by M or not
bool divisible(int M, int arr[], int index, int sum, int n){

   // Cheking the divisiblilty by M when the array ends
   if (index == n){
      if (sum % M == 0){
         return true;
      }
      return false;
   }
   
   // Checking the divisibility of sum + arr[index]
   if (divisible(M, arr, index + 1, sum + arr[index], n)){
      return true;
   }
   
   // Checking the divisibility of sum - arr[index]
   if (divisible(M, arr, index + 1, sum - arr[index], n)){
      return true;
   }
   return false;
}
int main(){
   int M = 4, arr[2] = {1, 5};
   if (divisible(M, arr, 0, 0, 2)){
      cout << "TRUE";
   }
   else{
      cout << " FALSE";
   }
   return 0;
}

输出

TRUE

时间复杂度 − 最坏情况下为 O(2^n),但在实践中,由于修剪了搜索空间,它比暴力法更好。

空间复杂度 − O(n),因为使用了递归栈空间。

方法 3:贪心算法

解决这个问题的贪心方法是首先按递增顺序对数组进行排序,然后如果总和不超过 M,则贪婪地应用加法函数。这种方法可能无法给出全局最优解,但会给出局部最优解。

伪代码

procedure divisible (M, arr[])
   sum = 0
   for i = 1 to end of arr
      sum = sum + arr[i]
   if sum is divisible by M
      ans = true
   end if
   sort array arr[]
   i = 0
   j = last index of array
   while i < j
      if arr[j] - arr[i] is divisible by M
         ans = true
      end if
      if sum % M == (sum - arr[j]) % M
         sum = sum - arr[j]
         j = j - 1
      else
         sum = sum - arr[i]
         i = i + 1
      end if
   ans = false
end procedure

示例:C++ 实现

在下面的程序中,对数组进行排序以找到可以被 M 整除的最优局部子数组。

#include <bits/stdc++.h>
using namespace std;

// Greedy function to find if a valid sequence is divisible by M or not
bool divisible(int M, vector<int> &arr){
   int sum = 0;
   for (int i = 0; i < arr.size(); i++) {
      sum += arr[i];
   }
   
   // Checking if sumof all elements is divisible by M
   if (sum % M == 0){
      return true;
   }
   
   sort(arr.begin(), arr.end());
   int i = 0, j = arr.size() - 1;
   while (i < j){
   
      // Checking if the difference between the largest and smallest element at a time in the array is divisible by M
      if ((arr[j] - arr[i]) % M == 0){
         return true;
      }
      
      // Removing either the largest or smallest element based on which does not affect the sum's divisibility
      if (sum % M == (sum - arr[i]) % M){
         sum -= arr[i];
         i++;
      }
      else{
         sum -= arr[j];
         j--;
      }
   }
   return false;
}
int main(){
   int M = 4;
   int array[2] = {1, 3};
   vector<int> arr(array, array + 2);
   if (divisible(M, arr)){
      cout << "TRUE";
   }
   else{
      cout << " FALSE";
   }
   return 0;
}

输出

TRUE

方法 4:动态规划

使用动态规划的概念,在这个解法中,我们存储评估的中间结果。我们将创建一个具有 N+1 行和 M 列的表,以及当我们不使用数组的任何元素时结果 % M == 0 的基本情况。然后迭代所有可能的 M 模余数,我们更新表。

伪代码

procedure divisible (arr[], M , N)
   dp[N+1][M] = false
   dp[0][0] = true 
   for i = 1 to N
      for i = j to M
         mod = arr[ i- 1] % M
         dp[i][j] = dp[i - 1][(j - mod + M) % M] or dp[i - 1][(j + mod) % M]
   ans = dp[N][0]
end procedure

示例:C++ 实现

在下面的程序中,我们将问题分解成子问题,然后解决它们。

#include <bits/stdc++.h>
using namespace std;

// Function to find if a valid sequence is divisible by M or not
bool divisible(int arr[], int M, int N){

   // Creating the dp table of size N+1 * M
   vector<vector<bool> > dp(N + 1, vector<bool>(M, false));
   
   // Base case
   dp[0][0] = true;
   
   // For each element iterating over all possible remainders j modulo M
   for (int i = 1; i <= N; i++){
      for (int j = 0; j < M; j++){
         int mod = arr[i - 1] % M;
         
         // Either exclude or include the current element in the table
         dp[i][j] = dp[i - 1][(j - mod + M) % M] || dp[i - 1][(j + mod) % M];
      }
   }
   return dp[N][0];
}
int main(){
   int M = 4;
   int arr[2] = {1, 3};
   if (divisible(arr, M, 2)){
      cout << "TRUE";
   }
   else{
      cout << " FALSE";
   }
   return 0;
}

输出

TRUE

结论

总之,为了找到可以被 M 整除的有效序列,我们可以应用多种方法,其时间和空间分析各不相同,从暴力法的 O(2^n) 到动态规划的 O(NM),动态规划是最有效的方法。

更新于:2023年7月25日

92 次浏览

开启您的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.