Coc Dsa Assignment 3
Coc Dsa Assignment 3
return result;
}
};
● Intuition:
We have to find the intersection of two arrays i.e. we need to
determine which elements appear in both arrays while ensuring
each element appears only once in the output.
● Approach:
1. Store Unique Elements: Converting nums1 into an
unordered_set<int>, which ensures each element appears
only once.
2. Check nums2 Elements: Iterating through nums2 and
checking if each element exists in the set.
○ If it does, adding it to the result vector.
○ Removing the element from the set to prevent
duplicates in the output.
● Algorithm:
1. Convert nums1 into an unordered_set<int> to store unique
values.
2. Initialize an empty vector result to store the intersection.
3. Iterate through nums2:
■ If the current number exists in the set:
● Add it to the result.
● Erase it from the set to prevent duplicates.
4. Return result.
● (Using a map):
○ Code:
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
unordered_map<int, int> freqMap;
vector<int> result;
for (int num : nums1) {
freqMap[num] = 1;
}
for (int num : nums2) {
if (freqMap[num] == 1) {
result.push_back(num);
freqMap[num] = 0;
}
}
return result;
}
};
● Intuition:
We need to find the intersection of two arrays, ensuring each
element appears only once in the output. Instead of using an
unordered_set, we utilize an unordered_map<int, int> (hash map)
to store the elements of nums1 as given. Then, we iterate through
nums2, checking if an element exists in the map with a value of 1. If
it does, we add it to the result and update its value to 0 to prevent
duplicates.
● Approach:
● (Using lower_bound/upper_bound):
● Code:
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
vector<int> result;
sort(nums1.begin(), nums1.end());
sort(nums2.begin(), nums2.end());
int i = 0, j = 0;
while (i < nums1.size() && j < nums2.size()) {
if (nums1[i] < nums2[j]) {
i++;
} else if (nums1[i] > nums2[j]) {
j++;
} else {
result.push_back(nums1[i]);
while (i < nums1.size() - 1 && nums1[i] == nums1[i + 1]) i++;
while (j < nums2.size() - 1 && nums2[j] == nums2[j + 1]) j++;
i++;
j++;
}
}
return result;
}
};
● Intuition:
● Approach:
1. Sort Both Arrays:
○ First, we sort nums1 and nums2 to make it easier to find
common elements in order.
2. Use Two Pointers:
○ Initialize two pointers, i = 0 (for nums1) and j = 0 (for nums2).
○ Traverse both arrays:
■ If nums1[i] < nums2[j], increment i (move to the next
element in nums1).
■ If nums1[i] > nums2[j], increment j (move to the next
element in nums2).
■ If nums1[i] == nums2[j], we found a common element:
■ Add it to result.
■ Skip duplicate elements in both arrays by
moving i and j to the next unique elements.
3. Return the Result:
○ After exiting the loop, return the result vector containing the
unique intersection elements.
● Algorithm:
1. Sort nums1 and nums2.
2. Initialize two pointers, i = 0 and j = 0.
3. Use a while loop to iterate through both arrays:
○ If nums1[i] < nums2[j], increment i.
○ If nums1[i] > nums2[j], increment j.
○ If nums1[i] == nums2[j], add nums1[i] to result and move
both pointers to skip duplicates.
● (Using 2 pointers):
○ Code:
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
vector<int> result;
sort(nums1.begin(), nums1.end());
sort(nums2.begin(), nums2.end());
int i = 0, j = 0;
while (i < nums1.size() && j < nums2.size()) {
if (nums1[i] < nums2[j]) {
i++;
} else if (nums1[i] > nums2[j]) {
j++;
} else {
result.push_back(nums1[i]);
while (i < nums1.size() - 1 && nums1[i] == nums1[i + 1]) i++;
while (j < nums2.size() - 1 && nums2[j] == nums2[j + 1]) j++;
i++;
j++;
}
}
return result;
}
};
● Intuition:
If we sort both arrays, any common elements will appear in the same relative
order. By using two pointers (one for each array), we can "walk" through the
arrays in sync and efficiently find matching elements.
Since we want only unique common elements, we must avoid adding duplicates.
○ Approach:
1. Sorting both arrays (nums1 and nums2).
2. Using two pointers i and j to traverse both arrays.
3. If nums1[i] == nums2[j], it's a common element:
○ Adding it to the result.
○ Skip all duplicates of nums1[i] and nums2[j].
4. If nums1[i] < nums2[j], increment i (move forward in nums1).
5. Else, increment j (move forward in nums2).
6. Repeat until the end of either array is reached.
5.
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
unordered_map<int, int> elementIndex;
for (int i = 0; i < nums.size(); i++) {
if (elementIndex.count(nums[i])) {
if (i - elementIndex[nums[i]] <= k) {
return true;
}
}
elementIndex[nums[i]] = i;
}
return false;
}
};
private:
int countSubarraysWithAtMostK(vector<int>& nums, int k) {
if (k == 0) return 0;
return count;
}
};