๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
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
Connected Components -> vector<int>vis(1e5+2,0);
vector<vector<int>>facs(1e5+2);
vector<int>par(1e5+2,-1),rnk(1e5+2,1);

int find(int n){
    if(par[n]<0){
        return n;
    }
    return par[n]=find(par[n]);
}

void unite(int a, int b){
    a=find(a);
    b=find(b);
   
    if(a==b){
        return;
    }
   
    if(rnk[a]>rnk[b]){
        swap(a,b);
    }
   
    par[a]=b;
    rnk[b]+=rnk[a];
   
}

void sieve(){
   
    for(int i=2;i<=1e5;i++){
        for(int j=2;j*j<=i;j++){
            if(i%j==0){
                facs[i].pb(j);
                if(j*j!=i){
                    facs[i].pb(i/j);
                }
            }
        }
        sort(facs[i].begin(),facs[i].end());
    }
   
}

void solve(){
   
    sieve();
   
    int n;
    cin>>n;
   
    vector<int>v(n);
    for(int i=0;i<n;i++){
        cin>>v[i];
    }
   
    for(int i=1e5;i>=2;i--){
        for(auto x:facs[i]){
            bool flag=0;
            int val;
            for(int j=0;j<n;j++){
                if(v[j]%x==0){
                    if(!flag){
                        flag=1;
                        val=j;
                    }
                    else{
                        unite(j,val);
                    }
                }
            }
        }
    }
   
    int cc=0;
    for(int i=0;i<n;i++){
        if(vis[find(i)]){
            continue;
        }
        else{
            vis[find(i)]=1;
            cc++;
        }
    }
   
    cout<<cc<<endl;
   
}

Connected Components โœ…
class Solution {
    public String convertToTitle(int col) {
        StringBuilder sb = new StringBuilder();
        while(col > 0){
            int rem = (col - 1) % 26;
            sb.append((char)(rem + 'A'));
            col = (col - 1) / 26;
        }
        return sb.reverse().toString();
    }
}

Map the Column โœ…
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int calcDist(int x) {
    if (x == 0) return -1;
   
    int max_dist = -1;
    int curr_dist = -1;
    int prev_pos = -1;
   
    int pos = 0;
    while (x > 0) {
        if (x & 1) {
            if (prev_pos != -1) {
                curr_dist = pos - prev_pos;
                max_dist = max(max_dist, curr_dist);
            }
            prev_pos = pos;
        }
        x >>= 1;
        pos++;
    }
   
    return max_dist;
}

vector<int> getTopKDistances(vector<int>& nums, int k) {
    vector<pair<int, int>> dists;
   
    for (int num : nums) {
        int d = calcDist(num);
        dists.push_back({d, num});
    }
   
    sort(dists.begin(), dists.end(), [](const pair<int, int>& a, const pair<int, int>& b) {
        if (a.first != b.first) {
            return a.first > b.first;
        } else {
            return a.second > b.second;
        }
    });
   
    vector<int> top_k;
    for (int i = 0; i < k; ++i) {
        top_k.push_back(dists[i].second);
    }
   
    return top_k;
}
Maximum bit differenceโœ…
WITH june_events AS (
  SELECT *
  FROM events
  WHERE dt >= '2022-06-01' AND dt < '2022-07-01'
),
grouped_data AS (
  SELECT
    mime,
    GROUP_CONCAT(DISTINCT SUBSTRING_INDEX(filename, '.', -1) ORDER BY SUBSTRING_INDEX(filename, '.', -1) SEPARATOR ', ') AS extension,
    COUNT(*) AS files,
    SUM(filesize) / (1024 * 1024) AS total_mib
  FROM june_events
  GROUP BY mime
)
SELECT
  mime,
  extension,
  files,
  CASE
    WHEN total_mib >= 1024 THEN CONCAT(ROUND(total_mib / 1024, 2), ' GiB')
    ELSE CONCAT(ROUND(total_mib, 2), ' MiB')
  END AS total
FROM grouped_data
ORDER BY total_mib DESC;


Download file type Report โœ…
import java.util.*;

public class LongestSequenceOfFollowers {
   
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long[] dp = new long[(int)(n + 1)];
        long m = 1000000007;
       
        dp[1] = 0; // Starting condition
       
        for (int i = 2; i <= n; i++) {
            if (i % 2 == 0) {
                dp[i] = (dp[i-1] * 3 + 3) % m;
            } else {
                dp[i] = (dp[i-1] * 3 - 3 + m) % m; // Ensure non-negative result before modulo
            }
        }
       
        System.out.println(dp[(int)n]);
    }
}

Number of paths โœ…
def inject_in_case(s, k):
    for i in range(len(s)):
        if k >= s[i]:
            return s[:i] + k + s[i:]
    return s + k
s = input().strip()
k = input().strip()

if len(k) == 1:
    result = inject_in_case(s, k)
    print(result)
#include <bits/stdc++.h>
using namespace s
td;
typedef long long
ll;
const int MOD = 1e9 +
9;

struct testca
se{
    testcase
(){
        int n; cin >>
n;
        vector<int> a(
n);
        for (int i=0; i<n; i++) cin >> a[
i];
        sort(a.begin(), a.end(
));
        vector<int> b(
n);
        for (int i=0; i<n; i++) cin >> b[
i];
        sort(b.begin(), b.end(), greater<>(
));
        ll result =
1;
        for (int i=0; i<n; i+
+){
            int geq_count = a.size() - (upper_bound(a.begin(), a.end(), b[i]) - a.begin(
));
            result = result * max(geq_count - i, 0) % M
OD;
     
}
        cout << result << "\
n";
 
}
};

int main
(){
    cin.tie(0)->sync_with_stdio(
0);
    int t; cin >>
t;
    while (t--) testcase
();
}


Count the Arrays โœ…
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
vector < vector < int > > adj;
    string letters;

    int dfs(int u, int p, int& best_path){
        // visit the current node
        int best_path_1 = 0, best_path_2 = 0;
       
        auto update_max = [&](int x){
            if(x >= best_path_1)
                best_path_2 = best_path_1, best_path_1 = x;
            else if(x >= best_path_2)
                best_path_2 = x;
        };

        for(auto& v : adj[u]){
            // if the adjacent node not visited and not the same letter let's calc it
            if(v == p) continue;
            int v_ans = dfs(v, u, best_path);
            if(letters[u] != letters[v])
                update_max(v_ans);
        }

        // take two branches
        best_path = max({best_path, best_path_1 + best_path_2 + 1});

        // the maximum path from the current node
        return 1 + best_path_1;
    }

    void add_edge(int u, int v){
        adj[u].push_back(v), adj[v].push_back(u);
    }

    int longestPath(vector<int>& parent, string& s) {
        // global variable initialization
        int n = parent.size();
        adj = vector < vector < int > > (n);
        letters = s;

        // add edges between i and parent of i
        for(int i = 1; i < n; i++)
            add_edge(i, parent[i]);
       
        int best_path = 0;
        dfs(0, -1, best_path);

        // the length of the longest path with the required conditions.
        return best_path;
    }


int main()
{
int n; cin >> n;
vector<int> parent(n);
for (auto & x : parent) cin >> x;
string s; cin >> s;
cout << longestPath(parent,  s) << endl;
}


Fantasy forest Pathfinding โœ…
#include <bits/stdc++.h>

using namespace s
td;

#define forn(i, n) for(int i = 0; i < int(n); i+
+)

const int INF = 1
e9;

int main
(){
int
n;
cin >>
n;
vector<string> s(
2);
forn(i, 2) cin >> s[
i];
vector<array<array<int, 2>, 2>> dp(n +
1);
forn(i, n + 1) forn(j, 2) forn(k, 2) dp[i][j][k] = -I
NF;
dp[0][0][s[1][0] == '1'] = s[1][0] == '
1';
dp[0][0][0] =
0;
forn(i, n - 1) forn(j,
2){
  int nxtj = s[j][i + 1] == '
1';
  int nxtj1 = s[j ^ 1][i + 1] == '
1';
  dp[i + 1][j ^ 1][0] = max(dp[i + 1][j ^ 1][0], dp[i][j][1] + nxtj
1);
  dp[i + 1][j][nxtj1] = max(dp[i + 1][j][nxtj1], dp[i][j][0] + nxtj1 + nxt
j);
  dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][0] + nxt
j);
}
cout << max({dp[n - 1][0][0], dp[n - 1][0][1], dp[n - 1][1][0], dp[n - 1][1][1]}) << '\
n';
}
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
class Solution { 
    int ring_size;
    unordered_map<char,vector<int>> mp;
    int clockwise(int curr, int new_pos){
        if(new_pos >= curr){
            return new_pos-curr;
        }
        return ring_size - (curr - new_pos);
    }
    int anti_clockwise(int curr, int new_pos){
        if(curr >= new_pos){
            return curr - new_pos;
        }
        return ring_size - (new_pos - curr);
    }
    int solve(string &key, int idx, int pos, vector<vector<int>>& dp){
        if(idx == key.size()){
            return 0; //end of key
        }
        if(dp[idx][pos] != -1){
            return dp[idx][pos];
        }
        int steps = INT_MAX;
        int key_value = key[idx];
        
        //going to all indexes
        for(int i = 0; i < mp[key_value].size(); i++){
            int new_pos = mp[key_value][i];
            int taken = solve(key,idx+1,new_pos,dp);
            //clockwise
            steps = min(steps,1+clockwise(pos,new_pos)+taken);
            //anticlockwise
            steps = min(steps,1+anti_clockwise(pos,new_pos)+taken);
        }
        return dp[idx][pos] = steps;
    }
public:
    int findRotateSteps(string& ring, string& key) {
        ring_size = ring.size();
        for(int i = 0; i < ring_size; i++){
            mp[ring[i]].push_back(i); 
        }

        vector<vector<int>> dp(key.size(),vector<int>(ring.size(),-1));
        return solve(key,0,0,dp);
    }
};


Uber โœ…
#include<bits/stdc++.h>
using namespace std;
const int N=6e6+10000;
int ch[N][2];
int sz[N];
int a[N];
int n,idx;
void insert(int x)
{
int p=0;
for(int i=29;i>=0;i--)
{
  int u=(x>>i)&1;
  if(!ch[p][u]) ch[p][u]=++idx;
  p=ch[p][u];
  ++sz[p];
}
}
int  dfs(int u)
{
int l=ch[u][0],r=ch[u][1];
if(l && r) return min(sz[l]-1+dfs(r),sz[r]-1+dfs(l));

if(l) return dfs(l);
if(r) return dfs(r);
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
  scanf("%d",&a[i]);
  insert(a[i]);
}
printf("%d\n",dfs(0));
}

uber โœ…
for ca in range(int(input())):
  f=True
  s=input()
  t=input()
  a=''
  for i in range(20):
    a+=s
    if len(a)%len(t)==0 and a==t*(len(a)//len(t)):
      print(a)
      f=False
      break
  if f:
    print('NO')

Swaraj
Uber โœ…
int minimumRemovals(vector<int>& arr) {
    int n = arr.size();
    int maxElement = *max_element(arr.begin(), arr.end());
    int minElement = *min_element(arr.begin(), arr.end());
   
    int maxIndex = find(arr.begin(), arr.end(), maxElement) - arr.begin();
    int minIndex = find(arr.begin(), arr.end(), minElement) - arr.begin();
   
    if (maxIndex > minIndex) swap(maxIndex, minIndex);
   
    int removalsFromLeft = minIndex + 1;
    int removalsFromRight = n - maxIndex;
    int removalsBothEnds = maxIndex + 1 + (n - minIndex);
   
    return min({removalsFromLeft, removalsFromRight, removalsBothEnds});
}

Minimum Removals โœ…
int solve(vector<int>& nums)
{
    int N = nums.size();
    int idx1 = min_element(nums.begin(), nums.end()) - nums.begin();
    int idx2 = max_element(nums.begin(), nums.end()) - nums.begin();
    int L = min(idx1, idx2);
    int R = max(idx1, idx2);
    int left = R + L;
    int right = N - L;
    int both = L + 1 + N - R;
    int ans = min({left, right, both});
    return ans;
}