GeeksForGeeks - POTD | GFG POTD Answer
Closed channel
1 218
Subscribers
No data24 hours
-97 days
-5730 days
Posts Archive
class Solution{
public:
int maxSweetness(vector& arr, int N, int K) {
int sum = 0;
for(int i=0;i>1;
int ct = K+1;
int sm = 0;
int i=0;
while(i=mid)
{
ct--;
sm=0;
}
i++;
}
if(ct<=0)
{
l = mid+1;
ans = max(ans,mid);
}
else{
h = mid-1;
}
}
return ans;
}
};
class Solution{
public:
set track;
//do dfs and create the preorder string from the root
void dfs(Node *root, string &s) {
if (!root) return;
s += to_string(root->data) + ",";
for (int i = 0; i < root->children.size(); i++) {
if (root->children[i] != NULL) {
dfs(root->children[i], s);
}
}
}
void helper(Node *root, set &ans) {
if (!root) return;
//do bfs
queue q;
q.push(root);
while (!q.empty()) {
int size = q.size();
//for every node create dfs string and check whether already covered
while (size--) {
Node* node = q.front();
string preorder = "";
dfs(node, preorder);
//if subtree is there previously add to ans
if (track.find(preorder) != track.end()) {
ans.insert(preorder);
} else {
//if subtee is not there then insert to track set
track.insert(preorder);
}
q.pop();
for (int i = 0; i < node->children.size(); i++) {
if (node->children[i] != NULL) {
q.push(node->children[i]);
}
}
}
}
}
int duplicateSubtreeNaryTree(Node *root) {
set ans;
helper(root, ans);
//ans is size of ans set
return ans.size();
}
};
Redeemed My 1st GFG Reward !!🎁❤️
Keep Solving Daily Challenge‼️
Keep Following GFG POTD ❤️
class Solution{
public:
int powerfullInteger(int n,vector> &in,int k){
int mx=0;
for(int i=0;iv(mx+2,0);
for(int i=0;i=k)ans=i;
}
return ans;
}
};
‼️Get ₹100 After Sign Up ‼️
‼️ACCOUNT ACTIVATION IN 24HRS⏰‼️
Hey there! Did you know why opening a DEMAT account with Paytm Money is so cool?
Here are the top 3 reasons to start trading on Paytm Stocks:
*FREE DEMAT account
*ZERO hidden charges
*LOW BROKERAGE charges and a HOST of EXCITING FEATURES
Download now: https://p.paytm.me/xCTH/bg716m2j
‼️ Any Query !! Write In Comments 💬
class Solution{
public:
long long findNumber(long long N){
long long ans = 0;
while(N > 0) {
int rank = N%5;
N /= 5;
if(rank == 0) {
rank = 5;
N--;
}
ans = ans*10 + (2*rank - 1);
}
return reverse(ans);
}
long long reverse(long long n) {
long long ans = 0;
while(n > 0) {
ans = ans*10 + n%10;
n /= 10;
}
return ans;
}
};
class Solution{
public:
vector> chefAndWells(int n,int m,vector> &c){
vector> ans(n,vector (m,0));
vector> vis(n,vector (m,0));
queue> q;
for(int i=0;i temp=q.front();
q.pop();
for(int k=0;k<4;k++){
int nr = temp.first+dr[k];
int nc = temp.second+dc[k];
if(nr>=0 && nc>=0 && nr
class Solution{
public:
vector easyTask(int n,string s,int q,vector> &queries){
vector ans;
for(const auto & query : queries){
if(query[0] == "1"){
int ind = stoi(query[1]);
s[ind] = query[2][0];
}
else if(query[0] == "2"){
int arr[26] = {0};
int left = stoi(query[1]);
int right = stoi(query[2]);
int k = stoi(query[3]);
for(int i=left; i<=right; i++){
arr[s[i]-'a'] += 1;
}
for(int i=25; i>=0; i--){
k -= arr[i];
if(k <= 0){
ans.push_back('a'+i);
break;
}
}
}
}
return ans;
}
};
class Solution{
public:
bool is_possible_to_get_seats(int n, int m, vector& seats){
int i=0,j=1,prev=-1;
if(n == 0) return true;
if(n > m) return false;
int k = n;
while(k > 0 and j < m){
if(prev != 1 and seats[i] == 0 and seats[j] == 0)
{
k--;prev = seats[j];seats[i] = 1;i=i+2;j=j+2;
}
else
{
prev = seats[i];i++,j++;
}
}
if(k != 0 and seats[m-2] == 0 and seats[m-1] == 0){
k--;
seats[m-1]=1;
}
if(k != 0)
return false;
return true;
}
};
class Solution{
public:
int mod;
vector mp;
Solution(){
mod=1e9+7;
mp.resize(31);
vectorprime={2,3,5,7,11,13,17,19,23,29};
for(int i=2; i<=30; ++i){
if(0 == i%4 0 == i%9 25 == i) continue;
int mask=0;
for(int j=0; j<10; ++j){
if(0 ==i%prime[j]) mask |= 1<>=1;
}
return ans;
}
int goodSubsets(vector &arr, int n){
int one=0;
vector dp(1024), cnt(31);
dp[0] =1;
for(int i: arr){
if(i == 1)
one++;
else if(mp[i]!=0)
cnt[i]++;
}
for(int i=0; i<31; ++i){
if(cnt[i] == 0) continue;
for(int j=0; j<1024; ++j){
if(0 != (j &mp[i]) ) continue;
dp[j | mp[i]] =
(int) ((dp[j | mp[i]] + dp[j] * (long)cnt[i]) % mod);
}
}
long ans=0;
for(int i:dp) ans = (ans+i) % mod;
ans--;
if(one!=0) ans=ans*pow(one)%mod;
return (int)ans;
}
};
class Solution {
public:
vector<int> nearestSmallerTower(vector<int> a) {
int i, n=a.size();
stack<int> st;
vector<int> ans(n, -1);
for(i=n-1;i>=0;i--) {
while(st.size() && a[st.top()]>=a[i]) st.pop();
ans[i]=(st.size())?st.top():-1;
st.push(i);
}
while(st.size()) st.pop();
for(i=0;i<n;i++) {
while(st.size() && a[st.top()]>=a[i]) st.pop();
if(st.size()) {
if(ans[i]==-1) ans[i]=st.top();
else {
int index=st.top();
if((i-index)<(ans[i]-i)) ans[i]=index;
else if((i-index)==(ans[i]-i)) {
if(a[index]<=a[ans[i]]) ans[i]=index;
}
}
}
st.push(i);
}
return ans;
}
};
