GEEKS FOR GEEKS SOLUTIONS🫢
Open in Telegram
1 342
Subscribers
No data24 hours
-27 days
-2930 days
Posts Archive
class Solution
{
public:
//Function to return list containing vertices in Topological order.
void topo(int node, vector<int>&vis, stack<int>& st, vector<int> adj[]){
vis[node]=1;
for(auto it: adj[node]){
if(!vis[it]){
topo(it,vis,st,adj);
}
}
st.push(node);
}
vector<int> topoSort(int N, vector<int> adj[])
{
stack<int> st;
vector<int> vis(N,0);
for(int i=0;i<N;i++){
if(vis[i]==0){
topo(i,vis,st,adj);
}
}
vector<int> ans;
while(!st.empty()){
ans.push_back(st.top());
st.pop();
}
return ans;
}
};
class Solution{
public:
int LargButMinFreq(int arr[], int n) {
// code here
mapmp;
for(int i = 0;i=i.second)
{
maxi = max(maxi,i.first);
miniFreq = i.second;
}
}
return maxi;
}
};
class Solution {
vector delrow{-1, 0, +1, 0};
vector delcol{0, -1, 0, +1};
public:
bool isWordExist(vector>& board, string word) {
// Code here
int i, j, n = board.size(), m = board[0].size();
int idx{0};
vector> vis(n, vector(m, 0));
for(i = 0;i> que;
que.push({i, j, 0});
while(!que.empty()){
auto elem = std::move(que.front());
que.pop();
int ro = elem[0];
int cl = elem[1];
idx = elem[2];
++idx;
if(idx >= word.length()){
return 1;
}
vis[ro][cl] = 1;
for(int l = 0;l<4;l++){
int row = delrow[l] + ro;
int col = delcol[l] + cl;
if(row >= 0 && row < n && col >= 0 && col < m && board[row][col] == word[idx]
&& !vis[row][col]){
que.push({row, col, idx});
}
}
}
}
}
}
return 0;
}
};
class Solution {
public:
bool func(string A,string B)
{
int i = 0;
int j = 0;
while(i= 'A' and A[i]<='Z' and B[j]>='A' and B[j]<='Z')
{
return false;
}
else
{
i++;
}
}
return j==B.size();
}
vector CamelCase(int N, vector D, string Pattern) {
vector ans;
for(auto c : D)
{
if(func(c,Pattern))
{
ans.push_back(c);
}
}
if(ans.empty())
{
vector bools;
bools.push_back("-1");
return bools;
}
return ans;
}
};
struct Node* _Middle(struct Node *head)
{
struct Node *slow_ptr = head;
struct Node *fast_ptr = head;
struct Node * temp = head;
if (head!=NULL)
{
while (fast_ptr != NULL && fast_ptr->next != NULL)
{
fast_ptr = fast_ptr->next->next;
temp = slow_ptr;
slow_ptr = slow_ptr->next;
}
temp->next = NULL;
return slow_ptr;
}
return NULL;
}
struct Node* reverseList(struct Node *head)
{
// code here
// return head of reversed list
Node *start = head;
start = start->next;
head->next = NULL;
while(start != NULL)
{
Node* previous = start;
start = start->next;
previous->next = head;
head = previous;
}
return head;
}
class Solution{
public:
struct Node* modifyTheList(struct Node *head)
{
//mid nikala.
struct Node* middle = _Middle(head);
//right mid reverse kiya
middle = reverseList(middle);
//subract + update value;
struct Node* l = head;
struct Node* r = middle;
struct Node* temp = head;
while(l != NULL)
{
int em = l->data;
l->data = r->data - l->data;
r->data = em;
l = l->next;
r=r->next;
if(l != NULL)
{
temp = l;
}
}
middle = reverseList(middle);
temp->next = middle;
return head;
}
};
class Solution{
public:
long long power(int n,int x){
if(x==0) return 1;
long long temp = n;
while(--x){
temp *= n;
}
return temp;
}
int dp[1001][1001];
const int M = 1000000007;
int numOfWays(int n, int x)
{
memset(dp,-1,sizeof(dp));
return ways(1,n,x);
}
int ways(int i,int n,int x){
long long p = power(i,x);
if(p>n) return 0;
if(p==n || n==0) return 1;
if(dp[i][n]!=-1) return dp[i][n];
int ans = 0;
ans = (ans + ways(i+1,n-p,x))%M;
ans = (ans + ways(i+1,n,x))%M;
return dp[i][n] = ans;
}
};
class Solution {
public:
void dfs(int index , string s , int target , vector&ans , string temp , long long prev ,
long long res){
//base case
if(index == s.size()){
if(res == target){
ans.push_back(temp);
}
return;
}
string st = "";
long long curr = 0;
for(int j = index; j index and s[index] == '0'){
break;
}
st += s[j];
curr = curr * 10 + (s[j] - '0');
if(index == 0){
dfs(j + 1, s , target , ans , temp + st , curr , curr);
}
else{
dfs(j + 1, s , target , ans , temp + "+" + st , curr , res + curr);
dfs(j + 1, s , target , ans , temp + "-" + st , -curr , res - curr);
dfs(j + 1, s , target , ans , temp + "*" + st , prev*curr ,
res - prev + prev*curr);
}
}
}
public:
vector addOperators(string s, int target) {
vectorans;
string temp = "";
long long prev = 0;
dfs(0 , s , target , ans , temp , prev , 0);
return ans;
}
};
class Solution {
public:
int getMaximum(int n, vector &arr) {
// code here
long long sum = 0;
for(int i=0;i
class Solution{
public:
Node* helpConstructBinaryTree(int pre[], int preMirror[], int i1, int i2, int len, unordered_map &ind){
// base case
if(len<1) return NULL;
if(len==1) return new Node(pre[i1]);
// recursion calling
Node* root=new Node(pre[i1]);
// ll - length of left subtree, lr - length of right subtree
int ll=ind[preMirror[i2+1]]-i1-1;
int lr=len-ll-1;
root->left=helpConstructBinaryTree(pre,preMirror,i1+1,i2+lr+1,ll,ind);
root->right=helpConstructBinaryTree(pre,preMirror,i1+lr+1,i2+1,lr,ind);
return root;
}
Node* constructBinaryTree(int pre[], int preMirror[], int n){
// like construct binary tree from inorder and preorder
unordered_map ind;
for(int i=0;i
class Solution {
public:
int solve(int N, vector p){
vector temp(N,0);
for(int i=1;i
