0% found this document useful (0 votes)
10 views9 pages

Coc Dsa Assignment 3

The document provides various methods to find the intersection of two arrays, including using a set, a map, sorting with two pointers, and a two-pointer approach. Each method is accompanied by code, intuition, approach, and complexity analysis. Additionally, it includes solutions for checking nearby duplicates and counting subarrays with a specific number of distinct integers.

Uploaded by

aryan jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views9 pages

Coc Dsa Assignment 3

The document provides various methods to find the intersection of two arrays, including using a set, a map, sorting with two pointers, and a two-pointer approach. Each method is accompanied by code, intuition, approach, and complexity analysis. Additionally, it includes solutions for checking nearby duplicates and counting subarrays with a specific number of distinct integers.

Uploaded by

aryan jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

COC DSA ASSIGNMENT 3

1. Intersection of Two Arrays:


●​ (Using a set):
●​ Code:
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
unordered_set<int> set1(nums1.begin(), nums1.end());
vector<int> result;

for (int num : nums2) {


if (set1.count(num)) {
result.push_back(num);
set1.erase(num);
}
}

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.

As given, we use an unordered_set . We first store all unique


elements of nums1 in the set. Then, we iterate through nums2 and
check if any element exists in the set. If it does, we add it to the
result and remove it from the set to avoid duplicate elements.

●​ 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.

3.​ Return the Result: After iterating through nums2, returning


the result vector.​

●​ 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.

●​ Time Complexity: O(n+m).


●​ Space Complexity:O(n)

●​ (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:

1.​ Store Unique Elements:


●​ Create an unordered_map<int, int> (freqMap) and
store each element of nums1 with a value of 1.
●​ This ensures that we only mark each unique number
once.​

2.​ Check nums2 Elements:


○​ Iterate through nums2 and check if the current
element exists in freqMap with a value of 1.
○​ If it does:​

■​ Add it to the result vector.


■​ Set freqMap[num] = 0 to avoid duplicates in the
output.​

3.​ Return the Result:​


●​ After iterating through nums2, return the result vector.
●​ Algorithm:
1.​ Create an unordered_map<int, int> (freqMap).
2.​ Iterate through nums1 and store each element in freqMap
with a value of 1.
3.​ Initialize an empty vector result to store the intersection.
4.​ Iterate through nums2:
○​ If the current number exists in freqMap with a value of
1:
■​ Add it to result.
■​ Update freqMap[num] = 0 to prevent
duplicates.
5.​ Return result

●​ Time Complexity: O(n+m).


●​ Space Complexity: O(n).

●​ (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:

We need to find the intersection of two arrays, ensuring that each


element appears only once in the output. Instead of using a hash
set or map, we take advantage of sorting and use a two-pointer
approach. By sorting both arrays first, we can efficiently compare
elements and avoid duplicate entries while optimizing the solution.

●​ 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.

​ ​ 4. Return the result vector.

●​ Time Complexity:O(nlogn + mlog m).


●​ Space Complexity:O(1)

●​ (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;
}
};

6.Subarrays with K Different Integers:


Code:
class Solution {
public:
int subarraysWithKDistinct(vector<int>& nums, int k) {
return countSubarraysWithAtMostK(nums, k) - countSubarraysWithAtMostK(nums, k - 1);
}

private:
int countSubarraysWithAtMostK(vector<int>& nums, int k) {
if (k == 0) return 0;

unordered_map<int, int> freq;


int left = 0, count = 0;

for (int right = 0; right < nums.size(); right++) {


freq[nums[right]]++;

while (freq.size() > k) {


freq[nums[left]]--;
if (freq[nums[left]] == 0) {
freq.erase(nums[left]);
}
left++;
}

count += (right - left + 1);


}

return count;
}
};

You might also like