Untitled
class Solution { public: int arithmeticTriplets(vector<int>& nums, int diff) { cin.tie(nullptr)->sync_with_stdio(0); unordered_set<int> mp(nums.begin(), nums.end()); int ans = 0; for (int it : nums) { if (mp.count(it - diff) && mp.count(it + diff)) { ans += 1; } } return ans; } }; ____________________________________________________________ class Solution { public: int arithmeticTriplets(vector<int>& nums, int diff) { cin.tie(nullptr)->sync_with_stdio(0); int ans = 0, sz = nums.size(); for (int i = 0; i < sz; ++i) { for (int j = i + 1; j < sz; ++j) { for (int k = j + 1; k < sz; ++k) { if (nums[j] - nums[i] == diff && nums[k] - nums[j] == diff) { ans += 1; } } } } return ans; } };
Leave a Comment