// Function to return length of longest subsequence of consecutive integers.
int longestConsecutive(vector<int>& arr) {
// Your code here
set<int> s(arr.begin(), arr.end());
int res = 0;
for(int i=0; i<arr.size(); i++) {
if(s.find(arr[i]) != s.end() && s.find(arr[i]-1) == s.end()) {
int count = 0, cur = arr[i];
while(s.find(cur) != s.end()) {
count++;
s.erase(cur);
cur++;
}
res = max(count, res);
}
}
return res;
}
Month: December 2024
GFG PTOD | 30 Dec | Union of Arrays with Duplicates
int findUnion(vector<int>& a, vector<int>& b) {
// code here
unordered_set<int> s(a.begin(), a.end());
for(int i=0; i<b.size(); i++) {
s.insert(b[i]);
}
return s.size();
}
GFG PTOD | 29 DEC | Intersection of Two arrays with Duplicate Elements
vector<int> intersectionWithDuplicates(vector<int>& a, vector<int>& b) {
// code here
vector<int> res;
unordered_set<int> s(a.begin(), a.end());
for(int j = 0; j<b.size(); j++) {
if(s.find(b[j]) != s.end()) {
res.push_back(b[j]);
s.erase(b[j]);
}
}
return res;
}
GFG PTOD | 28 Dec | Find All Triplets with Zero Sum
vector<vector<int>> findTriplets(vector<int> &arr) {
// Code here
// Set to handle duplicates
set<vector<int>> resSet;
int n = arr.size();
unordered_map<int, vector<pair<int, int>>> mp;
// Store sum of all the pairs with their indices
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++)
mp[arr[i] + arr[j]].push_back({i, j});
}
for (int i = 0; i < n; i++) {
// Find remaining value to get zero sum
int rem = -arr[i];
if (mp.find(rem) != mp.end()) {
vector<pair<int, int>> pairs = mp[rem];
for (auto p : pairs) {
// Ensure no two indices are same in triplet
if (p.first != i && p.second != i) {
vector<int> curr = {i, p.first, p.second};
sort(curr.begin(), curr.end());
resSet.insert(curr);
}
}
}
}
vector<vector<int>> res(resSet.begin(), resSet.end());
return res;
}
GFG PTOD | 27 Dec | Count pairs with given sum
int countPairs(vector<int> &arr, int target) {
// Code here
map<int, int> mp;
int count = 0;
for(int i=0; i<arr.size(); i++) {
if(mp.count(arr[i])){
count += mp.find(arr[i])->second;
}
mp[target - arr[i]]++;
}
return count;
}
GFG PTOD | 26 Dec | Two Sum – Pair with Given Sum
bool twoSum(vector<int>& arr, int target) {
// code here
std::set<int> s;
for(int i=0; i<arr.size(); i++) {
if(s.find(arr[i]) != s.end()) {
return true;
} else {
s.insert(target-arr[i]);
}
}
return false;
}
Set Matrix Zeroes
void setMatrixZeroes(vector<vector<int>> &mat) {
// code here
int row = mat.size(), col = mat[0].size();
bool isFirstRow = false, isFirstCol = false;
// check 0th row and 0th col, if value is 0
for(int j=0; j<col; j++) {
if(mat[0][j] == 0)
isFirstRow = true;
}
for(int i=0; i<row; i++) {
if(mat[i][0] == 0)
isFirstCol = true;
}
// mark from 1st row and 1st col to nth row and mth col
for(int i=1; i<row; i++) {
for(int j=1; j<col; j++) {
if(mat[i][j] == 0) {
mat[i][0] = 0;
mat[0][j] = 0;
}
}
}
// fill 0 from 1st row and 1st col to nth row and mth col
for(int i=1; i<row; i++) {
for(int j=1; j<col; j++) {
if(mat[i][0] == 0 || mat[0][j] == 0)
mat[i][j] = 0;
}
}
//fill the first row and first col to 0
if(isFirstRow) {
for(int j=0; j<col; j++)
mat[0][j] = 0;
}
if(isFirstCol) {
for(int i=0; i<row; i++)
mat[i][0] = 0;
}
}
GFG PTOD | 24 Dec | Search in a sorted Matrix
// Function to search a given number in row-column sorted matrix.
bool searchMatrix(vector<vector<int>> &mat, int x) {
// your code here
int i = 0, j=mat[0].size()-1;
while(i<mat.size() && j>=0) {
if(mat[i][j] == x) return true;
if(mat[i][j] > x) j--;
else i++;
}
return false;
}
GFG PTOD | 23 Dec | Search in a row-wise sorted matrix
bool searchRowMatrix(vector<vector<int>> &mat, int x) {
// code here
for(int i = 0; i<mat.size(); i++) {
int low = 0, high = mat[0].size();
while(low<=high) {
int mid = low + (high - low) / 2;
if(mat[i][mid] == x) return true;
if(mat[i][mid] < x)
low = mid+1;
else
high = mid-1;
}
}
return false;
}
GFG PTOD | 22 Dec | Search in a Row-Column sorted matrix
bool matSearch(vector<vector<int>> &mat, int x) {
// your code here
int i = 0, j=mat[0].size()-1;
while(i<mat.size() && j>=0) {
if(mat[i][j] == x) return true;
if(mat[i][j] > x) j--;
else i++;
}
return false;
}