Fastrail08

(V. IMPORTANT, CREATE SUBARRAYS WITH RECURSION)Arithmetic Slices 1(subarrays)

Oct 2nd, 2025
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. /*
  5. IMPORTANT THEORY-
  6. TO GENERATE EVERY SUBARRAYS FOR AN ARRAY via recursion, simply break into prefixes & suffixes, CALCULATE on the PREFIX and CALL on the SUFFIX
  7.  
  8. As we have generate subarrays, in recursion to generate subarrays is by generating prefixes and suffixes of range
  9. For a range [l......k......r] => [l, k] is the prefix of [l, r], and [k + 1, r] is the suffix of [l, r]
  10.  
  11. To generate each subarray of [l, r], generate each prefix of [l, r], and recursively call on the remaining suffix for every prefix.
  12.  
  13. In simple terms, for a range [l, r]
  14. Generate each prefix (by looping k from l + 1, r), and for each prefix[l, k], call on the suffix [k + 1, r].
  15. The current prefix of [l, r] is your SUBARRAY/SUBSTRING in CONSIDERATION / CURRENT SUBSTRING OR CURRENT SUBARRAY.
  16. So every CALCULATION will be done on PREFIXES, and every CALL will be done on the SUFFIXES.
  17.  
  18. */
  19.  
  20. int getAPCount(int l, int r, vector<int> &nums, vector<vector<int> > &memo){
  21.     // base case (it is not actually needed, because it is managed by the loop)
  22.     if(r - l < 2){
  23.         return 0;
  24.     }
  25.    
  26.     //memo check
  27.     if(memo[l][r] != -1){
  28.         return memo[l][r];
  29.     }
  30.     //Generate each prefix for range [l, r] by looping k from [l, r]
  31.      // prefix / current subarray [l, k]
  32.      // OBSERVATION - Common Difference / CD will be defined by the first two terms of the sequence of every valid subarray.
  33.      //so for a range [l, r], Every prefix of [l, r], the first two terms will l & l + 1, so C.D. = (l + 1)th - (l)
  34.      int count = 0;
  35.      int cd = nums[l + 1] - nums[l];
  36.     bool validPrefix = true;
  37.     for(int k = l; k <= r; k++){
  38.         //Once a prefix is INVALID, the other prefix formed at the current level are extension of it, so they will be invalid as well
  39.         if(k > 0 && nums[k] - nums[k - 1] != cd){
  40.             validPrefix = false;
  41.         }
  42.         //CALCULATE on the PREFIX
  43.         if(k - l >= 2 && nums[k] - nums[k - 1] == cd && validPrefix){
  44.             // if current subarray / current prefix is a VALID AP (size >= 3 and cd is consistent)
  45.             count += 1;
  46.         }
  47.         //CALL on the SUFFIX[k + 1, r] to generate rest of the subarrays & get their count by recursion.
  48.         count += getAPCount(k + 1, r, nums, memo);
  49.     }
  50.     return memo[l][r] = count;
  51. }
  52.  
  53. int numberOfArithmeticSlices(vector<int> &nums){
  54.     int n = nums.size();
  55.     vector<vector<int> >  memo(n, vector<int>(n, - 1));
  56.     return getAPCount(0, n - 1, nums, memo);
  57. }
  58.  
  59. int main() {
  60.     // your code goes here
  61.     int n;
  62.     cin >> n;
  63.     vector<int> nums(n);
  64.     for(int i = 0; i < n; i++){
  65.         cin >> nums[i];
  66.     }
  67.     cout << numberOfArithmeticSlices(nums) << '\n';
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment