๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
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
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;

int main() {
    int N;
    cin >> N;
    vector<int> A(N);
    for (int i = 0; i < N; i++) {
        cin >> A[i];
    }

    int maxA = *max_element(A.begin(), A.end());
    vector<long long> dp(maxA + 1, 0);
    vector<long long> count(maxA + 1, 0);

    for (int i = 0; i < N; i++) {
        count[A[i]]++;
    }
    for (int g = maxA; g >= 1; g--) {
        long long cnt = 0;
        for (int mul = g; mul <= maxA; mul += g) {
            cnt = (cnt + dp[mul]) % MOD;
        }
        cnt = (cnt + (1LL << count[g]) - 1) % MOD;
        dp[g] = cnt;
    }

    for (int i = 0; i < N; i++) {
        cout << dp[A[i]] << " ";
    }
    cout << endl;

    return 0;
}


gcd and bob โœ…
Winzo
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
vector<int> solve(int T, vector<long long>& test_cases) {
    vector<int> results;
    results.reserve(T);
   
    for (const auto& N : test_cases) {
        results.push_back(static_cast<int>(sqrt(N)));
    }
   
    return results;
}



FLIP FLOOP AND SWITCHESโœ…
Zenoti
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll solve(ll n)
{
    vector<ll>dp(n+10);
    dp[0]=1;
    dp[1]=2;
    dp[2]=5;
    ll mod=1e9+7;
    for(ll i=3;i<n;i++)
    {
        dp[i]=(2*dp[i-1]+dp[i-3])%mod;
    }
    return dp[n-1];
}
signed main()
{  
    ll n; cin>>n;
    cout<<solve(n);
    return 0;
}

Play with Tiles โœ…
Winzo
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
#include<stdio.h>
#include<stdlib.h>
long long arr[100005];
int cmpfunc (const void * a, const void * b)
{
   return ( *(int *)a - *(int *)b );
}
int main()
{
long long n,c,i,j,ans=0,a,sum=0;
scanf("%lld %lld",&n,&c);
for(i=0;i<n;i++)
{scanf("%lld",arr+i);sum+=arr[i];}
qsort(arr,n,sizeof(long long),cmpfunc);

for(i=0;i<n-1;i++)
{
sum-=arr[i];
ans+=sum-(arr[i]*(n-1-i));
}
if(n>1){
a=n/2;
ans+=a*(a+1)*c;
a=(n-1)/2;
ans+=a*(a+1)*c;
}
printf("%lld",ans);
return 0;
}

The Strange Array โœ…
Winzo
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> findMatrix(const vector<vector<int>>& a) {
    int n = a.size();
    int m = a[0].size();
        vector<vector<int>> prefix(n, vector<int>(m, 0));
    vector<vector<int>> b(n, vector<int>(m, 0));
        for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            prefix[i][j] = a[i][j];
            if (i > 0) prefix[i][j] += prefix[i-1][j];
            if (j > 0) prefix[i][j] += prefix[i][j-1];
            if (i > 0 && j > 0) prefix[i][j] -= prefix[i-1][j-1];
        }
    }

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            b[i][j] = prefix[i][j];
        }
    }
   
    return b;
}


Simple Matrix summation โœ…
๐Ÿ‘1
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
#include <bits/stdc++.h>
using namespace std;
#define ll long long
pair<ll,ll>bfs(ll n,ll u,vector<ll>adj[])
{
    ll dis[n];
    memset(dis,-1,sizeof(dis));
    queue<ll>q;
    q.push(u);
    dis[u] = 0;
    while (!q.empty()) {
        ll t = q.front();
        q.pop();
        for (auto v:adj[t])
        {
            if (dis[v] == -1)
            {
                q.push(v);
                dis[v]=dis[t]+1;
            }
        }
    }
    ll maxi=0;
    ll ind;
    for (ll i=0;i<n;i++)
    {
        if (dis[i]>maxi)
        {
            maxi=dis[i];
            ind=i;
        }
    }
    return {ind, maxi};

}
ll solve(ll n,vector<ll>&from,vector<ll>&to)
{  
    vector<ll>adj[n];
    for(ll i=0;i<from.size();i++)
    {
        adj[from[i]-1].push_back(to[i]-1);   
        adj[to[i]-1].push_back(from[i]-1);
    }
    pair<ll,ll>t1,t2;
    t1=bfs(n,0,adj);
    t2=bfs(n,t1.first,adj);
    return t2.second;
}
signed main()
{
    ll n,q; cin>>n>>q;
    vector<ll>from(q),to(q);
    for(ll i=0;i<q;i++) cin>>from[i];
    for(ll i=0;i<q;i++) cin>>to[i];
    cout<<solve(n,from,to);
}  

          ATLASSIAN 1โœ…
#define int long long
int countValidKs(int s) {
    int count = 0;
    for (int n = 1; n * (n - 1) / 2 < s; n++) {
        int num = s - (n * (n - 1)) / 2;
        if (num > 0 && num % n == 0) {
            count++;
        }
    }
    return count;
}


Atlassian 2โœ…
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>
using namespace std;
int dfs(int i, bool b1, bool b2, int sum_val, vector<vector<vector<vector<int>>>>& dp, const vector<char>& sv, const vector<char>& bv, int n) {
    if (sum_val < 0 || sum_val > 9 * (n - i))
        return 0;
    if (i == n)
        return sum_val == 0 ? 1 : 0;
    if (dp[i][b1][b2][sum_val] != -1)
        return dp[i][b1][b2][sum_val];
   
    int ans = 0;
    for (int j = 0; j < 10; ++j) {
        char j_char = '0' + j;
        if (!b1 && j_char < sv[i])
            continue;
        if (!b2 && j_char > bv[i])
            continue;
        ans += dfs(i + 1, b1 || (j_char > sv[i]), b2 || (j_char < bv[i]), sum_val - j, dp, sv, bv, n);
    }
    dp[i][b1][b2][sum_val] = ans;
    return ans;
}

// Main function to find the number of ways to choose the sum
vector<long> waysToChooseSum(long lowLimit, long highLimit) {
    string sv = to_string(lowLimit);
    string bv = to_string(highLimit);
    int n = max(sv.size(), bv.size());
    sv = string(n - sv.size(), '0') + sv;
    bv = string(n - bv.size(), '0') + bv;
   
    vector<vector<vector<vector<int>>>> dp(n, vector<vector<vector<int>>>(2, vector<vector<int>>(2, vector<int>(200, -1))));
   
    long fans = 0;
    long fsum = 0;
   
    for (int i = 0; i <= 10 * n; ++i) {
        int ans = dfs(0, false, false, i, dp, vector<char>(sv.begin(), sv.end()), vector<char>(bv.begin(), bv.end()), n);
        if (ans > fans) {
            fans = ans;
            fsum = 1;
        } else if (ans == fans) {
            ++fsum;
        }
    }
   
    return {fsum, fans};
}


BNY โœ…
WITH student_course_count AS (
    SELECT student_id, COUNT(*) AS course_count
    FROM Enrollments
    GROUP BY student_id
),
max_course_count AS (
    SELECT MAX(course_count) AS max_count
    FROM student_course_count
)
SELECT s.name
FROM Students s
JOIN student_course_count scc ON s.student_id = scc.student_id
JOIN max_course_count mcc ON scc.course_count = mcc.max_count
ORDER BY s.name;
#include <iostream>
#include <vector>
#include <map>
using namespace std;
class DisjointSet {
private:
    vector<int> parent, weight;
public:
    DisjointSet(int n) {
        parent.resize(n + 1);
        weight.resize(n + 1, 1);
        for (int node = 0; node <= n; node++) {
            parent[node] = node;
        }
    }

    int findParent(int node) {
        if (parent[node] != node) {
            parent[node] = findParent(parent[node]);
        }
        return parent[node];
    }

    int connect(int A, int B) {
        A = findParent(A);
        B = findParent(B);
        if (A == B) return 0;
        if (weight[A] == weight[B]) {
            weight[A] += weight[B];
            parent[B] = A;
        } else if (weight[A] < weight[B]) {
            weight[B] += weight[A];
            parent[A] = B;
        } else {
            weight[A] += weight[B];
            parent[B] = A;
        }
        return 1;
    }

    int count(int n) {
        int ans = 0;
        for (int node = 0; node <= n; node++) {
            if (parent[node] == node) {
                ans += weight[node] - 1;
            }
        }
        return ans;
    }
};

int getMinOperations(vector<int> data) {
    int n = data.size();
    map<int, int> mp;
    for (int i = 0, j = 0; i < n; i++) {
        if (!mp.count(data[i])) {
            mp[data[i]] = j++;
        }
    }
    for (int &num : data) {
        num = mp[num];
    }
    DisjointSet ds(n);
    for (int i = 0; i < n / 2; i++) {
        if (data[i] != data[n - i - 1]) {
            ds.connect(data[i], data[n - i - 1]);
        }
    }
    return ds.count(n);
}

BNY  getMinOperations โœ…
๐Ÿ‘1
int dfs(vector<pair<int, int>> g[], pair<int, int> disRev[], bool visit[], int u) {
    visit[u] = true;
    int totalRev = 0;
    for (int i = 0; i < g[u].size(); i++) {
        int v = g[u][i].first;
        if (!visit[v]) {
            disRev[v].first = disRev[u].first + 1;
            disRev[v].second = disRev[u].second;
            if (g[u][i].second) {
                disRev[v].second = disRev[u].second + 1;
                totalRev++;
            }
            totalRev += dfs(g, disRev, visit, v);
        }
    }
    return totalRev;
}

int getMinInversions(int e,vector<int>& from, vector<int>& to) {
    int V = e + 1;
    vector<pair<int, int>> g[V + 1];
    pair<int, int> disRev[V + 1];
    bool visit[V + 1];

    for (int i = 0; i < e; i++) {
        int u = from[i];
        int v = to[i];
        g[u].push_back(make_pair(v, 0));
        g[v].push_back(make_pair(u, 1));
    }

    for (int i = 1; i <= V; i++) {
        visit[i] = false;
        disRev[i].first = disRev[i].second = 0;
    }

    int root = 1;
    int totalRev = dfs(g, disRev, visit, root);
    int res = INT_MAX;

    for (int i = 1; i <= V; i++) {
        int edgesToRev = (totalRev - disRev[i].second) + (disRev[i].first - disRev[i].second);
        if (edgesToRev < res) {
            res = edgesToRev;
            root = i;
        }
    }

   return res;
}

Neutral Network โœ…
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
class FenwickTree:
    def __init__(self, n):
        self.n = n
        self.tree = [0] * (n + 1)
   
    def update(self, i, delta):
        while i <= self.n:
            self.tree[i] += delta
            i += i & (-i)
   
    def query(self, i):
        total = 0
        while i > 0:
            total += self.tree[i]
            i -= i & (-i)
        return total

def countSubsequences(arr, l, r):
    MOD = 10**9 + 7
   
    values = sorted(set(arr + [l-1, r]))
    value_map = {v: i+1 for i, v in enumerate(values)}
   
    ft = FenwickTree(len(value_map))
    result = 0
    prefix_mex = 0
    ft.update(value_map[prefix_mex], 1)
   
    for num in arr:
        if num < prefix_mex:
            ft.update(value_map[num], 1)
        else:
            while prefix_mex in value_map and ft.query(value_map[prefix_mex]) > 0:
                prefix_mex += 1
            if prefix_mex in value_map:
                ft.update(value_map[prefix_mex], 1)
       
        left = ft.query(value_map.get(l-1, 0))
        right = ft.query(value_map.get(r, 0))
        result = (result + (right - left)) % MOD
   
    return result