GeeksForGeeks - POTD | GFG POTD Answer
Closed channel
1 218
Subscribers
No data24 hours
-97 days
-5730 days
Posts Archive
class Solution
{
public:
string kthPermutation(int n, int k){
string s;
for(int i=1; i
class Solution {
public:
bool isPrime(int x){
if(x <= 1)
return false;
for(int i = 2; i <= sqrt(x); i++){
if(x%i == 0)
return false;
}
return true;
}
vector leastPrimeFactor(int n) {
vector ans(n+1, 2);
ans[1] = 1;
for(int i = 3; i
class Solution
{
public:
void findPreSuc(Node* root, Node*& pre, Node*& suc, int key)
{
// 1. Base case
if(root==NULL)
return;
// 2. Left call
findPreSuc(root->left,pre,suc,key);
// n - 3. codition check
if(root->key < key)
pre = root;
if(root->key > key and suc == NULL )
{
suc = root;
}
// 4. right call
findPreSuc(root->right,pre,suc,key);
}
};
class Solution
{
int ans;
public:
void fun(Node *root,int K){
if(root==NULL)
return;
ans=min(ans,abs(root->data-K));
if(Kdata)
fun(root->left,K);
else if(K>root->data)
fun(root->right,K);
else
return;
}
int minDiff(Node *root, int K)
{
ans=INT_MAX;
fun(root,K);
return ans;
}
};
class Solution
{
public:
string reverseEqn (string s)
{
//code here.
stack<string> st;
string temp = ""; // temporary string to push and pop numbers in the same order
for(int i = 0; i < s.length(); i++){
if(s[i] - '0' >= 0 && s[i] - '9' <= 0) temp += s[i]; // if it is a digit store it in temp
else{
// if it is not a number
if(temp.size()){
st.push(temp); // push the previously stored number on to the stack
temp = "";
}
st.push(string(1, s[i])); // push the current operator on the stack
}
}
// in case the last number is not pushed
if(temp.size()){
st.push(temp);
temp = "";
}
// pop the stack to obtain the reverse of the equation
string sol = "";
while(!st.empty()){
sol += st.top();
st.pop();
}
return sol;
}
};
class Solution{
public:
int maxEqualSum(int N1,int N2,int N3,vector &S1,vector &S2,vector &S3){
int i=0,j=0,k=0;
int sum1=0,sum2=0,sum3=0;
int ans=-1;
for(int i=0;isum2 && sum1>sum3)
sum1=sum1-S1[i++];
else if(sum2>sum1 && sum2>sum3)
sum2-=S2[j++];
else if(sum3>sum1 && sum3>sum1)
sum3-=S3[k++];
else if(sum2>sum1 && sum3>sum1)
{ sum2-=S2[j++];
sum3-=S3[k++];
}
else if(sum1>sum2 && sum3>sum2)
{ sum1=sum1-S1[i++];
sum3-=S3[k++];
}
else if(sum1>sum3 && sum2>sum3)
{ sum2-=S2[j++];
sum1=sum1-S1[i++];
}
}
return 0;
}
};
class Solution{
void dfs(vector> &g, vector &vis, int src, int &cnt){
vis[src] = true;
for(auto it : g[src]){
if(!vis[it]){
cnt += 1;
dfs(g, vis, it, cnt);
}
}
}
vectordata;
public:
void precompute()
{
vectorprime(1e6+1,true);
prime[0]=false;
prime[1]=false;
for(int i=2;i<1e6+1;i++)
{
if(prime[i]==true)
{
data.push_back(i);
int j=2*i;
while(j<1e6+1)
{
prime[j]=false;
j=j+i;
}
}
}
}
int helpSanta(int n, int m, vector> &g){
vector vis(n+1, false);
int k = 0;
for(int i=1; i<=n; i++){
if(!vis[i]){
int cnt = 1;
dfs(g, vis, i, cnt);
k = max(k, cnt);
}
}
if(k == 1) return -1;
return data[k-1];
}
};
class Solution
{
public:
//Function to return list containing vertices in Topological order.
vector topoSort(int V, vector adj[])
{
vector indegree(V, 0);
for(int i=0; i ans;
queue q;
vector vis(V, 0);
for(int i=0; i
class Solution{
public:
int LargButMinFreq(int arr[], int n) {
mapm;
for(int i=0;i
class Solution {
public:
int move[5] = {0, 1, 0, -1, 0};
bool dfs(int i, int j, int idx, vector>& board, string &word, int &m, int &n) {
if(idx == word.size()) return 1;
char ch = board[i][j];
board[i][j] = '0';
bool res = 0;
for(int k = 0; k < 4; k++) {
int newI = i + move[k];
int newJ = j + move[k + 1];
if(newI >= 0 && newI < m && newJ >= 0 && newJ < n &&
board[newI][newJ] != '0' && board[newI][newJ] == word[idx]) {
res |= dfs(newI, newJ, idx + 1, board, word, m, n);
}
}
board[i][j] = ch;
return res;
}
bool isWordExist(vector>& board, string word) {
int m = board.size();
int n = board[0].size();
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
if(board[i][j] == word[0]) {
if(dfs(i, j, 1, board, word, m, n)) return 1;
}
}
}
return 0;
}
};
