Problem
3718. Smallest Missing Multiple of K
Logic
- Insert all elements of
numsinto astd::setto remove duplicates and enable set operations. - Store the initial size of the set in a variable
length. - Iterate through positive multipliers
: - Insert
into the set. - If
st.size()changes (is not equal tolength), thenwas not originally in nums. - Return
immediately as the smallest missing multiple.
- Insert
Code
class Solution {
public:
int missingMultiple(vector<int>& nums, int k) {
set<int> st(nums.begin(), nums.end());
int length = st.size();
for (int i = 1; i < 102; i++) {
st.insert(k*i);
if (st.size() != length) {
return (k*i);
}
}
return k;
}
};