Advertisement
asdfg0998

dqasd

Oct 24th, 2024
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. const int MOD = 1e9 + 7;
  2. int generatePartitions(int index, const vector<int>& arr, int lastSumParity, vector<vector<int>>& dp) {
  3.     int n = arr.size();
  4.     if (index == n) return 1;
  5.  
  6.     if (dp[index][lastSumParity + 1] != -1) return dp[index][lastSumParity + 1];
  7.  
  8.     int sum = 0;
  9.     int count = 0;
  10.  
  11.     for (int i = index; i < n; i++) {
  12.         sum += arr[i];
  13.         int currentSumParity = sum % 2;
  14.         if (lastSumParity == -1 || currentSumParity != lastSumParity) {
  15.             count = (count + generatePartitions(i + 1, arr, currentSumParity, dp)) % MOD;
  16.         }
  17.     }
  18.     return dp[index][lastSumParity + 1] = count;
  19. }
  20.  
  21. int countValidPartitions(const vector<int>& arr) {
  22.     int n = arr.size();
  23.     vector<vector<int>> dp(n, vector<int>(3, -1));
  24.     return generatePartitions(0, arr, -1, dp);
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement