Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
647 B
2
Indexable
Never
class Solution {
public:
    int arithmeticTriplets(vector<int>& nums, int diff) {
        int ans = 0;

        for (int i = 0; i < nums.size(); i++)
        {
            for (int j = i + 1; j < nums.size() - 1; j++)
            {
                int validPair = nums[j] - nums[i];
                int k = diff + nums[j];

                if (validPair == diff)
                {
                    auto res = std::lower_bound(nums.begin() + j + 1, nums.end(), k);
                    if (res != nums.end() && *res == k)
                        ++ans;
                }
            }
        }
        return ans;
    }
};
Leave a Comment