Two Pointers
Two Pointers
txt
3Sum With Multiplicity.txt
3Sum.txt
4Sum.txt
Container With Most Water.txt
Count the Number of Good Partitions.txt
Longest Mountain in Array.txt
Longest Word in Dictionary through Deleting.txt
Longest_Substring_Without_Repeating_Characters.txt
Max Consecutive Ones III.txt
Maximum_Erasure_Value.txt
Minimize Maximum Pair Sum in Array.txt
Number of Subsequences That Satisfy the Given Sum Condition.txt
Push Dominoes.txt
Remove Duplicates from Sorted Array II.txt
Remove Duplicates from Sorted Array.txt
Reverse Vowels of a String.txt
Successful Pairs of Spells and Potions.txt
Two Sum II - Input array is sorted.txt
Valid Triangle Number.txt
/*
MY YOUTUBE VIDEO FOR THIS Qn :
https://www.youtube.com/watch?v=hGLjwiPRh0U&list=PLpIkg8OmuX-K6A0sEPFxOSJh4_AjCGAFf&inde
x=6
Company Tags : Facebook, Amazon, Microsoft, Oracle
Leetcode Link : https://leetcode.com/problems/3sum-closest/
*/
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int n = nums.size();
sort(nums.begin(), nums.end());
ans += (c1*c2);
i++;
j--;
} else {
int duplicates = j-i+1;
ans += (duplicates*(duplicates-1)/2);
break;
}
}
}
}
int threeSumMulti(vector<int>& nums, int target) {
ans = 0;
sort(nums.begin(), nums.end());
for(int i = 0; i<nums.size()-2; i++) {
twoSum(nums, i+1, target-nums[i]);
}
long K = 1e9+7;
return ans%K;
}
};
vector<int> cnt(101);
for(int& x:arr) {
cnt[x]++;
}
return ans%K;
}
};
/*
MY YOUTUBE VIDEO ON THIS Qn :
https://www.youtube.com/watch?v=_cBWWebTVpI&list=PLpIkg8OmuX-K6A0sEPFxOSJh4_AjCGAFf&ind
ex=4
Company Tags : Amazon, Facebook, Google
Question on GfG : Find triplets with zero sum
(https://practice.geeksforgeeks.org/problems/find-triplets-with-zero-sum/1)
Leetcode Qn Link : https://leetcode.com/problems/3sum/
*/
class Solution {
public:
void twoSum(vector<int>& nums, int k, vector<vector<int>>& result, int target) {
//Two pointer technique (Sorted array) Things you must not forget in interviews
int i = k, j = nums.size()-1;
while(i < j) {
if(nums[i]+nums[j] > target)
j--;
else if(nums[i] + nums[j] < target)
i++;
else {
result.push_back({-target, nums[i], nums[j]});
while(i < j && nums[i] == nums[i+1]) i++;
while(i < j && nums[j] == nums[j-1]) j--;
i++; //Things you must not forget in interviews
j--; //Things you must not forget in interviews
}
}
}
vector<vector<int>> threeSum(vector<int>& nums) {
if(nums.size() < 3) //Things you must not forget in interviews
return {};
vector<vector<int>> result;
sort(nums.begin(), nums.end());
for(int i = 0; i<nums.size()-2; i++) { //(i<nums.size()-2)Things you must not forget in interviews
if(i!= 0 && nums[i] == nums[i-1]) { //Things you must not forget in interviews
continue;
}
twoSum(nums, i+1, result, -nums[i]);
}
return result;
}
};
/*
Company Tags : Amazon, Microsoft, Google
Leetcode Link : https://leetcode.com/problems/4sum/
*/
class Solution {
public:
void twoSum(vector<vector<int>>& result, vector<int>& nums, int i, int j, int n, int target) {
int k = j+1, l = n-1;
while(k<l) {
if(nums[k] + nums[l] > target)
l--;
else if(nums[k] + nums[l] < target)
k++;
else {
result.push_back({nums[i], nums[j], nums[k], nums[l]});
while( k < n-1 && nums[k] == nums[k+1]) k++;
while(l > 0 && nums[l] == nums[l-1]) l--;
k++;
l--;
}
}
}
vector<vector<int>> fourSum(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
int n = nums.size();
vector<vector<int>> result;
if(n < 4)
return result;
for(int i = 0; i<n-3; i++) {
if(i > 0 && nums[i] == nums[i-1])
continue;
for(int j = i+1; j<n-2; j++) {
if(j > i+1 && nums[j] == nums[j-1])
continue;
int subTarget = target-(nums[i]+nums[j]);
twoSum(result, nums, i, j, n, subTarget);
}
}
return result;
}
};
/*
Company Tags : Bloomberg, Facebook, Google, Amazon, Adobe
Leetcode Link : https://leetcode.com/problems/container-with-most-water/
*/
int water = 0;
while(i<j) {
return water;
}
};
int water = 0;
while(i<j) {
//Then move towards large one because we can have better answer
if(height[i] <
while(i < j && height[i] <= h)
i++;
//Same, move towards large one because we can have better answer
while(i < j && height[j] <= h)
j--;
}
return water;
}
};
/*
MY YOUTUBE VIDEO ON THIS Qn : https://www.youtube.com/watch?v=fyyjpElDeuY
Company Tags : I will update soon
Leetcode Link : https://leetcode.com/problems/count-the-number-of-good-partitions/
*/
/************************************************************ C++
************************************************************/
//Approach (Using simple 2 Pointer)
//T.C : O(n)
//S.C : O(n)
class Solution {
public:
int M = 1e9 + 7;
int numberOfGoodPartitions(vector<int>& nums) {
int n = nums.size();
unordered_map<int, int> last_index; //number, last index
int i = 0;
int j = max(0, last_index[nums[0]]);
int result = 1;
while(i < n) {
if(i > j) { //we found one partition
result = (result*2)%M;
}
j = max(j, last_index[nums[i]]);
i++;
}
return result;
}
};
/************************************************************ C++
************************************************************/
//Approach (Using simple 2 Pointer)
//T.C : O(n)
//S.C : O(n)
import java.util.HashMap;
import java.util.Map;
class Solution {
int M = 1000000007;
int i = 0;
int j = Math.max(0, last_index.get(nums[0]));
int result = 1;
while (i < n) {
if (i > j) {
result = (int) ((result * 2L) % M);
}
j = Math.max(j, last_index.get(nums[i]));
i++;
}
return result;
}
}
/*
Compnay Tags : NIL
Leetcode Link : https://leetcode.com/problems/longest-mountain-in-array/
*/
class Solution {
public:
int longestMountain(vector<int>& A) {
int n = A.size();
if(n < 3)
return 0;
int mid = 1;
int result = 0;
while(mid < n-1) {
if(A[mid-1] < A[mid] && A[mid] > A[mid+1]) {
int start = mid-1;
int end = mid+1;
while(start > 0 && A[start] > A[start-1]) {
start--;
}
class Solution {
public:
string s1 = "";
//They wanted to check if you comeup with isSubsequence approach or not
bool isSubsequence(string& s2) {
int i = 0;
int j = 0;
int m = s1.length();
int n = s2.length();
return j==n;
}
string findLongestWord(string s, vector<string>& d) {
string result = "";
s1 = s;
for(string str:d) {
if(isSubsequence(str)) {
if(str.length() > result.length())
result = str;
else if(str.length() == result.length() && str < result)
result = str;
}
}
return result;
}
};
/*
Company Tags : Morgan Stanley, Amazon, Microsoft, Housing.com
Leetcode Link : https://leetcode.com/problems/longest-substring-without-repeating-characters/
*/
int n = s.length();
int i = 0;
int j = 0;
while(j < n) {
if(!st.count(s[j])) {
st.insert(s[j]);
l = max(l, (int)st.size());
j++;
} else {
st.erase(s[i]);
i++;
/*
Example : "a b c d e a a"
When you are at 5th index, you notice that you saw ’a’ somewhere before, you delete it from
set
Now, you add this ’a’ (5th index) in the set and move ahead at 6th index.
Now, you see that ’a’ (6th index) is somewhere behind because it’s in the set.
But you keep deleting from set until you delete this ’a’ (5th index) from the set.
So, effectively you are iterating array 2 times in worst case. So, time = O(2*n)
*/
}
}
return l;
}
};
int i = 0, j = 0;
unordered_map<char, int> mp;
while(j < n) {
char ch = s[j];
if(!mp.count(ch)) {
//If not in map, update map and calculate length
mp[ch] = j;
res = max(res, j-i+1);
} else {
//if already present in map, make proper i jump and then calculate length
i = max(i, mp[ch] + 1);
mp[ch] = j; //Don’t forget to update new index of ch
res = max(res, j-i+1);
}
j++;
}
return res;
}
};
//Approach-3 (Just simplifying Approach-2. Notice some statements are common in if and else block.
Take them outside
//Time : O(n)
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int n = s.length();
int res = 0;
int i = 0, j = 0;
unordered_map<char, int> mp;
while(j < n) {
char ch = s[j];
if(mp.count(ch)) {
i = max(i, mp[ch] + 1);
}
mp[ch] = j;
res = max(res, j-i+1);
j++;
}
return res;
}
};
/*
Company Tags : Google (With some Variations)
Leetcode Link : https://leetcode.com/problems/max-consecutive-ones-iii/
*/
if(k >= n)
return n;
int result = 0;
int count = 0;
int i = 0, j = 0;
while(i < n) {
if(nums[i] == 0) {
count++;
moveUntilOneCount(nums, count, i, j, k);
}
result = max(result, i-j+1);
i++;
}
return result;
}
};
if(k >= n)
return n;
int result = 0;
int count = 0;
int i = 0, j = 0;
while(i < n) {
if(nums[i] == 0)
count++;
if(count > k) {
count -= nums[j] == 0 ? 1 : 0; //decrement k only if you are crossing leaving 0 behind
j++;
}
result = max(result, i-j+1);
i++;
}
return result;
}
};
/*
Leetcode Weekly Contest Qn
Company Tag : NIL (Let me know if you find any :-) )
Leetcode Link : https://leetcode.com/problems/maximum-erasure-value/
*/
//Approach-1 (Smart)
class Solution {
public:
int maximumUniqueSubarray(vector<int>& nums) {
int n = nums.size();
vector<int> cus(n+1, 0);
for(int i = 0; i<n; i++)
cus[i+1] = cus[i] + nums[i];
return maxS;
}
};
int sum = 0;
int i = 0, j = 0;
unordered_map<int, int> mp;
vector<int> cumS(n, 0);
cumS[0] = nums[0];
for(int i = 1; i<n; i++) {
cumS[i] = cumS[i-1] + nums[i];
}
while(i < n) {
if(!mp.count(nums[i])) {
mp[nums[i]] = i;
i++;
} else {
if(mp[nums[i]] >= j) {
int temp = cumS[i-1] - (j>0 ? cumS[j-1] : 0);
sum = max(sum, temp);
j = mp[nums[i]] + 1;
}
mp[nums[i]] = i;
i++;
}
}
int temp = 0;
if(j < n) {
temp = cumS[n-1] - (j>0 ? cumS[j-1] : 0);
}
/***************************************************************** C++
******************************************************************************/
//Simple 2-pointer approach
//T.C : O(nlogn) due to sorting
//S.C : O(1)
class Solution {
public:
int minPairSum(vector<int>& nums) {
sort(begin(nums), end(nums));
int maxResult = 0;
int i = 0, j = nums.size()-1;
while(i < j) {
int sum = nums[i] + nums[j];
maxResult = max(maxResult, sum);
i++;
j--;
}
return maxResult;
}
};
/***************************************************************** JAVA
******************************************************************************/
//Simple 2-pointer approach
//T.C : O(nlogn) due to sorting
//S.C : O(1)
class Solution {
public int minPairSum(int[] nums) {
Arrays.sort(nums);
int maxResult = 0;
int i = 0, j = nums.length - 1;
while (i < j) {
int sum = nums[i] + nums[j];
return maxResult;
}
}
/*
MY YOUTUBE VIDEO ON THIS Qn : https://www.youtube.com/watch?v=eGqs55VTP3I
Company Tags : Amazon
Leetcode Link :
https://leetcode.com/problems/number-of-subsequences-that-satisfy-the-given-sum-condition/
*/
//Approach-1 (Produce all subsequences, find min and max and check)
//This will give TLE
vector<int> power(n);
power[0] = 1;
//2. l = 0, r = n-1
int l = 0, r = n-1;
int result = 0;
while(l <= r) {
return result;
}
};
/*
Company Tags : Google
Leetcode Link : https://leetcode.com/problems/push-dominoes/
*/
if(s[i] == s[j])
result += string(midPartLength, s[i]);
else if(s[i] == ’L’ && s[j] == ’R’)
result += string(midPartLength, ’.’);
else
result += string(midPartLength/2, ’R’) + string(midPartLength%2, ’.’) + string(midPartLength/2,
’L’);
i = j;
}
return result;
}
};
vector<int> rightClosestL(n);
vector<int> leftClosestR(n);
return result;
}
};
vector<int> forces(n);
//Move from left to right and look for right force ’R’
int force = 0; //initially
for(int i = 0; i<n; i++) {
if(dominoes[i] == ’R’)
force = n; //My max power towards Right starts from here and it will decrease as I progress
else if(dominoes[i] == ’L’)
force = 0; //I can’t give force towards Right :-(
else
force = max(force-1, 0); //I told ya, my power decreases as I progress and hit a ’.’
forces[i] = force;
}
//Move from right to left and look for left force ’L’
force = 0; //initially
for(int i = n-1; i>=0; i--) {
if(dominoes[i] == ’L’)
force = n; //My max power towards Left starts from here and it will decrease as I progress
else if(dominoes[i] == ’R’)
force = 0; //I can’t give force towards Left :-(
else
force = max(force-1, 0); //I told ya, my power decreases as I progress and hit a ’.’
//Now I will find resultant direction on each domino basis of resultant force on them
string result(n ,’.’);
for(int i = 0; i<n; i++) {
if(forces[i] < 0)
result[i] = ’L’;
else if(forces[i] > 0)
result[i] = ’R’;
}
return result;
}
};
/*
Company Tags : Google, Facebook, Expedia, Microsoft
Leetcode Link : https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/
*/
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int n = nums.size();
/**************************************************************** C++
****************************************************************/
//T.C : O(n)
//S.C : O(1)
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int n = nums.size();
if(n == 0) return 0;
int i = 0, j = 1;
while(j < n) {
if(nums[i] != nums[j]) nums[++i] = nums[j];
j++;
}
return i+1;
}
};
/**************************************************************** JAVA
****************************************************************/
//T.C : O(n)
//S.C : O(1)
public class Solution {
public int removeDuplicates(int[] nums) {
int n = nums.length;
if (n == 0) return 0;
int i = 0, j = 1;
while (j < n) {
if (nums[i] != nums[j]) {
nums[++i] = nums[j];
}
j++;
}
return i + 1;
}
}
/*
MY YOUTUBE VIDEO ON THIS Qn : https://www.youtube.com/watch?v=pqzvpK8zTHU
Company Tags : Google, Zoho, Flipkart,
Leetcode Link : https://leetcode.com/problems/reverse-vowels-of-a-string/
GfG Link : https://practice.geeksforgeeks.org/problems/reversing-the-vowels5304/1
*/
while(i < j) {
if(!isVowel(s[i])) i++;
else {
swap(s[i], s[j]);
i++;
j--;
}
}
return s;
}
};
/*
MY YOUTUBE VIDEO ON THIS Qn : <soon>
Company Tags : MICROSOFT
Leetcode Link : https://leetcode.com/problems/successful-pairs-of-spells-and-potions/
*/
// Sort the ’spells with index’ and ’potions’ array in increasing order.
sort(sortedSpells.begin(), sortedSpells.end());
sort(potions.begin(), potions.end());
vector<int> answer(spells.size());
int m = potions.size();
int potionIndex = m - 1;
while (potionIndex >= 0 && (long long) spell * potions[potionIndex] >= success) {
potionIndex -= 1;
}
answer[index] = m - (potionIndex + 1);
}
return answer;
}
};
/*
MY YOUTUBE VIDEO FOR THIS Qn : https://www.youtube.com/watch?v=gsSLay-wcaw
Company Tags : Amazon
Leetcode Link : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/
*/
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
int i = 0, j = numbers.size()-1;
while(i < j) {
if(numbers[i]+numbers[j] == target) {
return {i+1, j+1};
}
sort(begin(nums), end(nums));
int count = 0;
for(int i = 0; i<n-2; i++) {
for(int j = i+1; j<n-1; j++) {
int k = j+1;
int sum = nums[i] + nums[j];
if(k < n) {
int idx = lower_bound(begin(nums)+k, end(nums), sum) - begin(nums);
count += (idx-j-1);
}
}
}
return count;
}
};
sort(begin(nums), end(nums));
int count = 0;
for(int i = n-1; i>1; i--) {
int l = 0, r = i-1;
while(l < r) {
if(nums[l] + nums[r] > nums[i]) {
count += (r-l);
r--;
} else {
l++;
}
}
}
return count;
}
};
/*
Note : If we use 3-SUM approach like as shown below :
for(int i = 0; i<n-2; i++) {
int l = i+1, r = n-1;
while(l < r) {
///code
}
}
2 2 3 4
i l r
nums[l]+nums[r] > nums[r], we increment count by 1
2 2 3 4
l r i
Here also, {2, 2, 3} is valid until l < r
*/