LeetCode, GeeksForGeeks Problem of the day solution
Open in Telegram
Complete daily challenges from LeetCode, GeeksForGeeks and redeem their rewards Channel link : https://t.me/leetcode_gfg_potd
Show more1 250
Subscribers
+224 hours
+147 days
+2930 days
Posts Archive
class Solution {
public:
void matchPairs(int n, char nuts[], char bolts[]) {
unordered_map<char,int> mp;
mp['^']=1; mp['@']=2;
mp['?']=3; mp['*']=4;
mp['&']=5; mp['%']=6;
mp['$']=7; mp['#']=8;
mp['!']=9;
priority_queue<pair<int,char>> pq;
for(int i=0; i<n; i++){
pq.push({mp[nuts[i]],nuts[i]});
}
for(int i=0; i<n; i++){
nuts[i]=bolts[i]=pq.top().second;
pq.pop();
}
}
};
class Solution {
public:
#define ll long long
int subarraysDivByK(vector<int>& nums, int k) {
vector<ll> mp(k);
ll tot = 0; int ans = 0;
mp[0] = 1;
for(int num: nums){
tot += num;
int mod = (tot % k + k) % k;
mp[mod]++;
}
for (int n: mp)
ans += (n * (n-1) * 0.5);
return ans;
}
};
class Solution {
public:
void zigZag(int n, vector &arr) {
for (int i = 0; i < n - 1; i++) {
if (i % 2 == 0) {
if (arr[i] > arr[i + 1]) {
swap(arr[i], arr[i + 1]);
}
} else {
if (arr[i] < arr[i + 1]) {
swap(arr[i], arr[i + 1]);
}
}
}
}
};
class Solution
{
public:
bool checkSubarraySum(vector& nums, int k)
{
unordered_mapm;
int sum=0;
for(int i=0;i0)
{
return true;
}
if(m.find(rem)==m.end())
{
m[rem]=i;
}
else if(i-m[rem]>=2)
{
return true;
}
}
return false;
}
};
class Solution {
public:
int findExtra(int n, int arr1[], int arr2[]) {
int i=0;
int ans=0;
while(ans==0){
ans=arr1[i]-arr2[i];
i++;
}
ans=i-1;
return ans;
}
};
class Solution {
public:
string replaceWords(vector& dictionary, string sentence) {
int count=0;
string s,t;
vectorv;
for(int i=0;i
class Solution {
public:
// l and r are input array
// maxx : maximum in r[]
// n: size of array
// arr[] : declared globally with size equal to maximum in l[] and r[]
// Function to find the maximum occurred integer in all ranges.
int maxOccured(int n, int l[], int r[], int maxx) {
// Your code here
unordered_mapx;
unordered_mapy;
vectorans(maxx+1,0);
int answer = 0;
int occ = 0;
for(int i=0 ;iocc)
{
occ = ans[i];
answer = i;
}
}
return answer;
}
};
class Solution {
public:
bool isNStraightHand(vector& hand, int groupSize) {
int totalSize = hand.size();
if(totalSize % groupSize != 0)
return false;
map mp;
for(auto it : hand) {
mp[it]++;
}
while(mp.size()) {
int num = mp.begin()->first;
for(int i = 0; i < groupSize; i++) {
if(mp[num+i]) {
mp[num+i]--;
if(mp[num+i] == 0) {
mp.erase(num+i);
}
}
else {
return false;
}
}
}
return true;
}
};
class Solution {
public:
long long max_sum(int a[], int n) {
long long s1 = 0;
long long ts = 0;
for (int i = 0; i < n; i++) {
s1 += (long long)i * a[i];
ts += (long long)a[i];
}
long long maxi = s1;
for (int i = n - 1; i >= 1; i--) {
s1 = s1 + ts - (long long)n * a[i];
if (maxi < s1) {
maxi = s1;
}
}
return maxi;
}
};
class Solution {
public:
vector commonChars(vector& words) {
vector ans;
map m2;
for(char ch : words[0]){
m2[ch]++;
}
for(int i = 1; i < words.size(); i++){
map m1;
for(char ch : words[i]){
m1[ch]++;
}
for(auto it = m2.begin(); it != m2.end();){
if(m1.find(it->first) != m1.end()){
it->second = min(it->second, m1[it->first]);
++it;
} else {
it = m2.erase(it);
}
}
}
for(auto it : m2){
string ch(1, it.first);
int x = it.second;
while(x--){
ans.push_back(ch);
}
}
return ans;
}
};
