๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
9.63K subscribers
5.61K photos
3 videos
95 files
10.6K links
๐ŸšฉMain Group - @SuperExams
๐Ÿ“Job Updates - @FresherEarth

๐Ÿ”ฐAuthentic Coding Solutions(with Outputs)
โš ๏ธDaily Job Updates
โš ๏ธHackathon Updates & Solutions

Buy ads: https://telega.io/c/cs_algo
Download Telegram
using ll = long long;

class SegTree {
    vector<ll> tree;
    vector<ll> A;
    ll n;

public:
    SegTree(const vector<int>& arr) {
        n = arr.size();
        A = vector<ll>(arr.begin(), arr.end());
        tree.resize(4 * n, 0);
        build(0, 0, n - 1);
    }

    void build(ll node, ll start, ll end) {
        if (start == end) {
            tree[node] = A[start];
        } else {
            ll mid = (start + end) / 2;
            build(2 * node + 1, start, mid);
            build(2 * node + 2, mid + 1, end);
            tree[node] = tree[2 * node + 1] + tree[2 * node + 2];
        }
    }

    void upd(ll idx, ll val, ll node, ll start, ll end) {
        if (start == end) {
            A[idx] = val;
            tree[node] = val;
        } else {
            ll mid = (start + end) / 2;
            if (idx <= mid) {
                upd(idx, val, 2 * node + 1, start, mid);
            } else {
                upd(idx, val, 2 * node + 2, mid + 1, end);
            }
            tree[node] = tree[2 * node + 1] + tree[2 * node + 2];
        }
    }

    ll qry(ll L, ll R, ll node, ll start, ll end) {
        if (R < start || end < L) {
            return 0;
        }
        if (L <= start && end <= R) {
            return tree[node];
        }
        ll mid = (start + end) / 2;
        ll left = qry(L, R, 2 * node + 1, start, mid);
        ll right = qry(L, R, 2 * node + 2, mid + 1, end);
        return left + right;
    }

    void upd(ll idx, ll val) {
        upd(idx, val, 0, 0, n - 1);
    }

    ll qry(ll L, ll R) {
        return qry(L, R, 0, 0, n - 1);
    }
};

vector<ll> solve(int N, int l, vector<int> A, vector<vector<int>> query) {
    SegTree segTree(A);
    vector<ll> results;

    for (const auto& q : query) {
        if (q[0] == 1) {
            int idx = q[1];
            int val = q[2];
            segTree.upd(idx - 1, val);
        } else if (q[0] == 2) {
            int L = q[1];
            int R = q[2];
            ll sum = 0;
            for (int i = L - 1; i < R; ++i) {
                for (int j = i; j < R; ++j) {
                    sum += segTree.qry(i, j);
                }
            }
            results.push_back(sum);
        }
    }

    return results;
}

Source : Hola
๐Ÿ‘2โค1
#include <bits/stdc++.h>
using namespace std;
#define ll long long
void dfs2(vector<ll>g[],ll v,ll p,vector<vector<ll>>&dp,vector<ll>&a,ll k)
{
    dp[v][0]=a[v];
    dp[v][1]=a[v]^k;
    for(auto it:g[v])
    {
        if(it!=p)
        {
            dfs2(g,it,v,dp,a,k);
            dp[v][0]+=dp[it][0];
            dp[v][1]+=dp[it][1];
        }
    }
}
void dfs(vector<ll>g[],ll v,ll p,vector<vector<ll>>&dp,ll&ans)
{
    ans=max(ans,dp[1][0]-dp[v][0]+dp[v][1]);
    for(auto it:g[v])
    {
        if(it!=p)
        {
            ans=max(ans,dp[1][1]-dp[it][1]+dp[it][0]);
            dfs(g,it,v,dp,ans);
        }
    }
}
ll solve(ll n,ll k,vector<ll>&a,vector<vector<ll>>&e)
{
    vector<ll>g[n+10];
    ll i;
    assert(1<=n and n<=1e5);
    for(ll i=0;i<n;i++)
    {
        ll u=e[i][0];
        ll v=e[i][1];
        g[v].push_back(u);
        g[u].push_back(v);
    }
   a.insert(a.begin(),0);
   vector<vector<ll>>dp(n+10,vector<ll>(2));
   ll ans=0;
   dfs2(g,1,0,dp,a,k);
   ans=max(ans,dp[1][0],dp[1][1]);
   dfs(g,1,0,dp,ans);
   return ans;
}


Xor Subtree โœ…
Google
๐ŸŽ‰1
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n, k;
    cin >> n >> k;

    map<int, int> mp;
    for (int i = 0; i < n; ++i) {
        int l, r, x;
        cin >> l >> r >> x;
        mp[l] += x;
        mp[r + 1] -= x;
    }

    vector<int> al;
    int sum = 0;
    for (auto& e : mp) {
        sum += e.second;
        al.push_back(sum);
    }

    int ans = 0, maxi = 0;
    unordered_map<int, int> lp;
    for (int i = 0; i < al.size(); ++i) {
        int num = al[i];
        int curr = lp[num - k] + 1;
        lp[num] = max(lp[num], curr);
        if (ans < lp[num] || (ans == lp[num] && maxi > num)) {
            maxi = num;
            ans = lp[num];
        }
    }

    cout << ans << " ";
    for (int i = ans-1; i >=0; i--) {
        cout << maxi - i * k << " ";
    }
    cout << "\n";

    return 0;
}


complex subsequence โœ…
#include<bits/stdc++.h>
using namespace std;
    
int findMessages (int N, vector<string> A) {
    int ans=N;
    set<string> st;

    for(int i=0; i<N; i++) {
        string s;
        for(int j=0; j<A[i].size(); j++)
            s += 'a' + ('z' - A[i][j]);
       
        if (st.find(s) != st.end())
            ans -= 1;

        st.insert(A[i]);
    }

    return ans;
}
    
int main() {
    
    ios::sync_with_stdio(0);
    cin.tie(0);
    int N;
    cin >> N;
    vector<string> A(N);
    for(int i_A = 0; i_A < N; i_A++)
    {
        cin >> A[i_A];
    }
    
    int out_;
    out_ = findMessages(N, A);
    cout << out_;
}

Alice and messages โœ…
vector<string> solve(string t, int Q, vector<string> q) {
    stringstream ss(t);
    string tk;
    vector<string> p;
    while (getline(ss, tk, '\"')) {
        p.push_back(tk);
    }

    vector<string> r;
    for (string qry : q) {
        size_t pos = qry.find_last_of('.');
        qry = qry.substr(pos + 1);
        int j = 0;
        for (j = 0; j < p.size(); j++) {
            if (p[j] == qry) {
                break;
            }
        }
        r.push_back(p[j + 2]);
    }
    return r;
}

Jason praising โœ…
โค1
def solve(N, A, D, V):
    info_index = {
        'bank_account_number': 0,
        'account_holder_first_name': 1,
        'account_holder_last_name': 2,
        'registered_mobile_number': 3,
        'branch_code': 4
    }
   
    filtered_accounts = [account for account in A if str(account[info_index[D]]) == V]
    sorted_accounts = sorted(filtered_accounts, key=lambda x: int(x[0]))

    output = "\n".join([" ".join(map(str, account)) for account in sorted_accounts])
    return output

N = int(input())
A = []
for _ in range(N):
    account_info = input().split()
    account_info[0] = int(account_info[0])
    account_info[3] = int(account_info[3])
    account_info[4] = int(account_info[4])
    A.append(account_info)

D = input()
V = input()

result = solve(N, A, D, V)
print(result)

Searching google pay saved accountโœ…
#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n; cin >> n;
    vector<char>decode(62);
    for (int i = 0; i < 10; ++i)
    {
        decode[i] = (i + '0');
    }
    for (int i = 10; i <= 35 ; ++i)
    {
        decode[i] = ((i - 10) + 'A');
    }
    for (int i = 36; i <= 61 ; ++i)
    {
        decode[i] = ((i - 36) + 'a');
    }

    string res = "";
    while (n) {
        int temp = n % 62;
        res += decode[temp];
        n /= 62;
    }
    reverse(res.begin(), res.end());
    cout << res << endl;
}

Encode it โœ…
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Phone {
    int price;
    int speed;
};

bool comparePhones(const Phone &a, const Phone &b) {
    if (a.price == b.price) {
        return a.speed > b.speed;
    }
    return a.price < b.price;
}

int main() {
    int N;
    cin >> N;

    vector<Phone> phones(N);
    for (int i = 0; i < N; ++i) {
        cin >> phones[i].price >> phones[i].speed;
    }

    int Q;
    cin >> Q;

    while (Q--) {
        int L, R;
        cin >> L >> R;

        vector<Phone> filteredPhones;
        for (const auto &phone : phones) {
            if (phone.price >= L && phone.price <= R) {
                filteredPhones.push_back(phone);
            }
        }

        sort(filteredPhones.begin(), filteredPhones.end(), comparePhones);

        cout << filteredPhones.size() << " mobiles are available" << endl;
        for (const auto &phone : filteredPhones) {
            cout << "Price " << phone.price << " Speed " << phone.speed << endl;
        }
    }

    return 0;
}. 

// SEARCH  FILTER TRY :) โœ…
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
try this edit: line 2 mai, ans=0; Maximum length subarray Google โœ…
bool p(vector<ll>a,ll mid,ll x,ll y,ll n){
    priority_queue<pair<ll,ll>>p;
    ll sum=0;
    set<pair<ll,ll>>s;
    for(int i=0;i<mid;i++){
        p.push({a[i],i});
        sum +=a[i];
    }
   
    while(y>0){
       sum-=p.top().first;
       s.insert({p.top().first,p.top().second});
       p.pop();
       y--;
    }
    if(sum<=x){
        return true;
    }
    for(ll i=mid;i<n;i++){
        sum -=a[i-mid];
        sum +=a[i];
        if(s.find({a[i-mid],i-mid})!=s.end()){
            s.erase({a[i-mid],i-mid});
        }
        p.push({a[i],i});
        auto it=s.begin();
        if(it!=s.end()){
            if(p.top().first>it->first){
                auto it1=p.top();
                sum -=p.top().first;
                p.pop();
                s.erase(it);
                s.insert(it1);
                p.push({it->first,it->second});
                int x1=it->first;
               
                sum +=x1;
               
            }
        }
        while(s.size()<y && !p.empty()){
            if(p.top().second<=i-mid){
                p.pop();
                continue;
            }
            else{
                s.insert({p.top().first,p.top().second});
               
                sum-=p.top().first;
                p.pop();
            }
        }
        if(sum<=x)
         return true;
    }
    return sum<=x;
}
int main() {
    ll n;cin>>n;
    ll x,y;cin>>x>>y;
    ll l=y+1,r=n;
    vector<ll>a;
    for(ll i=0;i<n;i++){
        ll x;cin>>x;
        a.pb(x);
    }
    ll ans=min(y,n);
    while(l<=r){
        ll mid=(l+r)/2;
        if(p(a,mid,x,y,n)){
            ans=mid;
            l=mid+1;
        }
        else{
            r=mid-1;
        }
    }
    cout<<ans<<endl;
}
๐Ÿ‘2
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
vector<int> solve(vector<int>& direction, vector<int>& strength) {
    int n = direction.size();
    stack<int> st;
    stack<int> in; 
    for (int i = 0; i < n; ++i) {
        int cudr = direction[i];
        int cs = strength[i];
        if (cudr == 1) {
            st.push(i);
        } else {
            while (!in.empty()) {
                int ii = in.top();
                int is = strength[ii];
               
                if (cs > is) {
                    in.pop();
                } else if (cs == is) {
                    in.pop();
                    cs = 0;
                } else {
                    cs = 0;
                    break;
                }
            }
           
            if (cs > 0) {
                st.push(i);
            }
        }
       
        in.push(i);
    }
    vector<int> result;
    while (!st.empty()) {
        result.push_back(st.top());
        st.pop();
    }
    reverse(result.begin(), result.end());
   
    return result;
}.

// BALL COLISIONโœ…
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
#include <vector>
#include <queue>
#include <climits>
using namespace std;
int getMinimumStress(int graph_nodes, vector<int> graph_from, vector<int> graph_to, vector<int> graph_weight, int source, int destination) {
    vector<vector<pair<int, int>>> adj(graph_nodes + 1);
    for (int i = 0; i < graph_from.size(); ++i) {
        int u = graph_from[i];
        int v = graph_to[i];
        int w = graph_weight[i];
        adj[u].emplace_back(v, w);
        adj[v].emplace_back(u, w);
    }

    vector<int> res(graph_nodes + 1, INT_MAX);
    res[source] = 0;

    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
    pq.emplace(0, source);

    while (!pq.empty()) {
        auto [cs, node] = pq.top();
        pq.pop();

        if (node == destination) {
            return cs;
        }

        for (auto& [neighbor, weight]: adj[node]) {
            int ns = max(cs, weight);
            if (ns < res[neighbor]) {
                res[neighbor] = ns;
                pq.emplace(ns, neighbor);
            }
        }
    }

    return res[destination] == INT_MAX ? -1 : res[destination];
}.

// MINIMIZE PATH VALUE โœ…
#include <iostream>
#include <vector>
#include <unordered_set>
#include <unordered_map>
using namespace std;
vector<bool> processQueries(const vector<vector<int>>& grid, const vector<int>& queries) {
    int n = grid.size();
    int m = grid[0].size();
    vector<vector<unordered_set<int>>> dp(n, vector<unordered_set<int>>(m));
    dp[0][0].insert(grid[0][0]);
        for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (i > 0) {
                for (int sum : dp[i-1][j]) {
                    dp[i][j].insert(sum + grid[i][j]);
                }
            }
            if (j > 0) {
                for (int sum : dp[i][j-1]) {
                    dp[i][j].insert(sum + grid[i][j]);
                }
            }
        }
    }
        vector<bool> results;
    for (int x : queries) {
        if (dp[n-1][m-1].count(x)) {
            results.push_back(true);
        } else {
            results.push_back(false);
        }
    }
   
    return results;
}.

ORACLE PATH SUM โœ…
#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
#include <limits>
using namespace std;
struct Edge {
    int to;
    int cost;
};
struct Node {
    int vertex;
    int cost;
        bool operator<(const Node& other) const {
        return cost < other.cost;
    }
};

int maxCostRoute(int n, int m, vector<vector<int>>& routes) {
    vector<vector<Edge>> graph(n + 1);
    for (const auto& route : routes) {
        int u = route[0];
        int v = route[1];
        int cost = route[2];
        graph[u].push_back({v, cost});
    }

    priority_queue<Node> pq;
    pq.push({1, 0});
   
    vector<int> maxCost(n + 1, numeric_limits<int>::min());
    maxCost[1] = 0;

    while (!pq.empty()) {
        Node current = pq.top();
        pq.pop();
        int currentVertex = current.vertex;
        int currentCost = current.cost;
        if (currentVertex == n) {
            return currentCost;
        }

        for (const auto& edge : graph[currentVertex]) {
            int nextVertex = edge.to;
            int nextCost = currentCost + edge.cost;

            if (nextCost > maxCost[nextVertex]) {
                maxCost[nextVertex] = nextCost;
                pq.push({nextVertex, nextCost});
            }
        }
    }

    return -1;
}. 

ORCALE  AIRLINE PROBLEM โœ…
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
void inorder(TreeNode* root, vector<int>& nodes) {
    if (root == nullptr) return;
    inorder(root->left, nodes);
    nodes.push_back(root->val);
    inorder(root->right, nodes);
}
TreeNode* findKthSmallest(TreeNode* root, int& k) {
    if (root == nullptr) return nullptr;
    TreeNode* left = findKthSmallest(root->left, k);
    if (left != nullptr) return left;
    if (--k == 0) return root;
    return findKthSmallest(root->right, k);
}
void inorderSubtree(TreeNode* root, vector<int>& nodes) {
    if (root == nullptr) return;
    inorderSubtree(root->left, nodes);
    nodes.push_back(root->val);
    inorderSubtree(root->right, nodes);
}
int findMedian(TreeNode* root, int K) {
    vector<int> nodes;
    inorder(root, nodes);
    int k = K;
    TreeNode* kthSmallestNode = findKthSmallest(root, k);
    if (kthSmallestNode == nullptr) return -1;
    vector<int> subtreeNodes;
    inorderSubtree(kthSmallestNode, subtreeNodes);
    int n = subtreeNodes.size();
    if (n % 2 == 1) {
        return subtreeNodes[n / 2];
    } else {
        return (subtreeNodes[n / 2 - 1] + subtreeNodes[n / 2]) / 2;
    }
}.

// ORACLE STRAGE MEDIUMโœ…
def MonkeyVsHooman(N, H, X, SI):
    if SI[X-1] >= H:
        return 1
    if N == 1:
        return -1
    def help(arr):
        n = len(arr)
        arr.append(float('inf'))
        mxJump = lambda x: arr[x] - min(arr[x - 1], arr[x + 1])
        cur, steps = 0, 0
        res = float('inf')
        for i in range(0, n, 2):
            cur = max(0, cur - (i and arr[i - 1]))
            cur += arr[i]
            steps += (1 if i == 0 else 2)
            jump = mxJump(i)
            if jump > 0:
                moves = (H - cur + jump - 1) // jump
                res = min(res, steps + moves * 2)
        return res

    ans = min(help(SI[X-1:]), help(SI[ :X][ ::- 1]))
    return ans if ans != float('inf') else -1

print(MonkeyVsHooman(5,10,1,[3,1,2,2,5]))


Monkeys vs hoomns โœ