Advertisement
asdfg0998

cs

Oct 24th, 2024
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. const int MOD = 1e9 + 7;
  2.  
  3. bool isValidPartition(const vector<int>& partitionSums) {
  4.     for (int i = 1; i < partitionSums.size(); i++) {
  5.         if (partitionSums[i] % 2 == partitionSums[i - 1] % 2) {
  6.             return false;
  7.         }
  8.     }
  9.     return true;
  10. }
  11.  
  12. void generatePartitions(int index, const vector<int>& arr, vector<int>& currentPartitionSums, int& count) {
  13.     int n = arr.size();
  14.  
  15.     if (index == n) {
  16.         if (isValidPartition(currentPartitionSums)) {
  17.             count = (count + 1) % MOD;
  18.         }
  19.         return;
  20.     }
  21.  
  22.     int sum = 0;
  23.     for (int i = index; i < n; i++) {
  24.         sum += arr[i];
  25.         currentPartitionSums.push_back(sum);
  26.         generatePartitions(i + 1, arr, currentPartitionSums, count);
  27.         currentPartitionSums.pop_back();
  28.     }
  29. }
  30.  
  31. int countValidPartitions(vector<int>& arr) {
  32.     int count = 0;
  33.     vector<int> currentPartitionSums;
  34.  
  35.     generatePartitions(0, arr, currentPartitionSums, count);
  36.  
  37.     return count;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement