en
Feedback
LeetCode, GeeksForGeeks Problem of the day solution

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 more
1 250
Subscribers
+224 hours
+147 days
+2930 days
Posts Archive
class Solution { public: int maxFrequencyElements(vector& nums) { int freq=0; int sum=0; sort(nums.begin(),nums.end()); int n=nums.size(); int i=0; while(i freq){ freq=cnt; sum=freq; } else if(freq==cnt){ freq=cnt; sum+=cnt; } i=j; } return sum; } };

GFG | Problem of the day :

class Solution{ public: bool sameFreq(string s) { vector mp(26,0); for(auto i:s) { mp[i-'a']=mp[i-'a']+1; } int f=mp[s[0]-'a']; int count=0; for(int i=0;i<26;i++) { if(mp[i]>0 && f!=mp[i]){ if(abs(mp[i]-f)==1) { count++; if(count==2){ return false; } } else if(abs(mp[i]-f)!=1) { return false; } } } return true; } };

GFG | Problem of the day :

class Solution { public: ListNode* middleNode(ListNode* head) { ListNode* slow = head; ListNode* fast = head; while(fast != NULL && fast->next != NULL){ slow = slow->next; fast = fast->next->next; } return slow; } };

LeetCode | Daily challenge :

class Solution { public: string longestSubstring(string str, int n) { vector> dp(n+1,vector(n+1,0)); int maxLen = 0, index = 0; for(int i=1;i maxLen) { maxLen = dp[i][j]; index = i-maxLen; } } } } } if(maxLen == 0) return "-1"; else return str.substr(index,maxLen); } };

GFG | Problem of the day :

class Solution { public: bool hasCycle(ListNode *head) { ListNode * slow = head; ListNode * fast = head; while(fast != NULL && fast->next != NULL){ slow = slow->next; fast = fast->next->next; if(slow==fast){ return true; } } return false; } };

LeetCode | Daily challenge :

class Solution { public: int asc(char c){ return c-'a'+1; } vector search(string pattern, string text) { //code here. vector ans; int l=0; int n=text.size(); int m=pattern.size(); int s=0; int p=0; for(int i=0;i

GFG | Problem of the day :

class Solution { public: int minimumLength(string s) { int i = 0, j = s.length() - 1; while (i < j) { char st = s[i]; if (s[i] != s[j]){break;} while (s[j] == st && j > i) { j--; } while (s[i] == st && i <= j) { i++; } } return j - i + 1; } };

LeetCode | Daily challenge :

class Pair { public: int value; int index; Pair(int value, int index) : value(value), index(index) {} bool operator<(const Pair& p) const { return this->value < p.value; } }; class Solution{ public: int maxIndexDiff(int a[], int n) { // Your code here vector list; for(int i = 0; i < n; ++i) list.push_back(Pair(a[i], i)); sort(list.begin(), list.end()); int ls = list.size(); int maxi_j = list[ls - 1].index; int maxi = 0; for(int i = ls - 2; i >= 0; --i) { if(list[i].index < maxi_j) { maxi = max(maxi, maxi_j - list[i].index); } maxi_j = max(maxi_j, list[i].index); } return maxi; } };

GFG | Problem of the day :

class Solution { public: int bagOfTokensScore(vector& tokens, int power) { int score = 0; sort(tokens.begin(), tokens.end()); int i = 0, j = tokens.size()-1; while(i <= j){ if(power >= tokens[i]){ power -= tokens[i]; ++i; ++score; } else if(score && i != j){ --score; power += tokens[j]; --j; } else break; } return score; } };

LeetCode | Daily challenge :

class Solution{ public: void swapElements(int arr[], int n){ for(int i=0;i

GFG | Problem of the day :