Skip to content

Instantly share code, notes, and snippets.

@shunting314
Created November 22, 2025 20:02
Show Gist options
  • Select an option

  • Save shunting314/6e3553970548259a60cabb316709f350 to your computer and use it in GitHub Desktop.

Select an option

Save shunting314/6e3553970548259a60cabb316709f350 to your computer and use it in GitHub Desktop.
class Solution {
public:
int numberOfArithmeticSlices(vector<int>& nums) {
int out = 0;
int s = 0;
while (s + 1 < nums.size()) {
int dif = nums[s + 1] - nums[s];
int e = s + 2;
while (e < nums.size() && nums[e] - nums[e - 1] == dif) {
++e;
}
int len = e - s;
// count
out += (len - 2) * (len - 1) / 2;
s = e - 1;
}
return out;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment