๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
9.63K subscribers
5.59K photos
3 videos
95 files
10.2K 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;
void bfs(int a, vector<vector<int>>& b, vector<bool>& c) {
    queue<int> d;
    d.push(a);
    c[a] = true;
    while (!d.empty()) {
    int e = d.front();
    d.pop();
    for (int f = 0; f < b.size(); ++f) {
    if (b[e][f] == 1 && !c[f]) {
    c[f] = true;
    d.push(f);
            }
        }
    }
}
int solve(int a, int b, vector<vector<int>>& c, vector<int>& d) {
    int e = INT_MAX, f = -1;
    for (int g : d) {
    vector<bool> h(a, false);
    vector<vector<int>> i = c;
    for (int j = 0; j < a; ++j) {
    i[g][j] = 0;
    i[j][g] = 0;
    }
    int k = 0;
    for (int l : d)
    {
    if (!h[l]) {
    bfs(l, i, h);
    }
    }
    k = count(h.begin(), h.end(), true);
    if (k < e || (k == e && g < f))
    {
    e = k;
    f = g;
    }
    }
    return f;
}

int main() {
    int a, b;
    cin >> a >> b;
    vector<vector<int>> c(a, vector<int>(a));
    for (int d = 0; d < a; ++d) {
    for (int e = 0; e < a; ++e)
    {
            cin >> c[d][e];
    }
    }
    vector<int> d(b);
    for (int e = 0; e < b; ++e)
    {
    cin >> d[e];
    }
    cout << solve(a, b, c, d) << endl;
    return 0;
}

Reduce the damage
Zeta โœ…
โค1
import math
def solve(l, r):
    count = 0
    for x in range(l, r + 1):
        sqrt_x = math.isqrt(x)
        if x % sqrt_x == 0: 
            count += 1
    return count

Lucky number โœ…
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
class CommunicationException(Exception):
    def __init__(self, message):
        super().__init__(message)

class Caller:
    def __init__(self, name):
        self.name = name

class CommsHandlerABC:
    def connect(self, user1, user2):
        pass

    def hangup(self, user1, user2):
        pass

    def clear_all(self):
        pass

class CommsHandler(CommsHandlerABC):
    def __init__(self, users):
        self.connected_users = set()
        self.user_objects = [Caller(user) for user in users]

    def connect(self, user1_idx, user2_idx) -> str:
        user1 = self.user_objects[user1_idx]
        user2 = self.user_objects[user2_idx]

        if user1.name == user2.name:
            raise CommunicationException(f"{user1.name} cannot connect with {user2.name}")

        if self.is_line_in_use():
            raise CommunicationException("Connection in use. Please try later")

        connection = frozenset([user1.name, user2.name])
        if connection in self.connected_users:
            raise CommunicationException(f"Connection between {user1.name} and {user2.name} already exists")

        self.connected_users.add(connection)
        return f"Success: Connection established between {user1.name} and {user2.name} "

    def hangup(self, user1_idx, user2_idx) -> str:
        user1 = self.user_objects[user1_idx]
        user2 = self.user_objects[user2_idx]

        if user1.name == user2.name:
            raise CommunicationException(f"{user1.name} cannot hangup with {user2.name}")

        connection = frozenset([user1.name, user2.name]) 
        if connection in self.connected_users:
            self.connected_users.remove(connection)
            return f"Success: {user1.name} and {user2.name} are disconnected "
        else:
            raise CommunicationException(f"{user1.name} and {user2.name} not found in the communication channel")

    def clear_all(self):
        self.connected_users.clear()

    def is_line_in_use(self) -> bool:
        return len(self.connected_users) >= 2


MSCI โœ…
def FilledBuckets(N, queries):
    filled = False
    clear_even = False
    clear_odd = False

    for query in queries:
        if query == 1:
            filled = True
            clear_even = False
            clear_odd = False
        elif query == 2 and filled:
            clear_even = True
        elif query == 3 and filled:
            clear_odd = True
        elif query == 4:
            filled = False
            clear_even = False
            clear_odd = False

    if not filled:
        return 0

    result = N
    if clear_even:
        result -= N // 2
    if clear_odd:
        result -= (N + 1) // 2
    return result

Fractal โœ…
def sp(K, L, R):
    MOD = 93179
    MAX = 20000
    is_p = [True] * (MAX + 1)
    is_p[0] = is_p[1] = False
    for i in range(2, int(MAX**0.5) + 1):
        if is_p[i]:
            for j in range(i * i, MAX + 1, i):
                is_p[j] = False

    sp_primes = [1] * (MAX + 1)
    for i in range(2, MAX + 1):
        if is_p[i]:
            d_sum = sum(int(d) for d in str(i))
            if d_sum % K == 0:
                sp_primes[i] = i
            else:
                sp_primes[i] = 1
        else:
            sp_primes[i] = 1

    prf = [1] * (MAX + 1)
    for i in range(1, MAX + 1):
        prf[i] = (prf[i - 1] * sp_primes[i]) % MOD

    res = []
    for l, r in zip(L, R):
        if l > 1:
            p = (prf[r] * pow(prf[l - 1], MOD - 2, MOD)) % MOD
        else:
            p = prf[r]
        res.append(p if p > 1 else 1)

    return res


Tengen and numbers โœ…
#include <bits/stdc++.h>
using namespace std;
int countSetBits(int num) {
    return __builtin_popcount(num);
}

void solve() {
    int n;
    cin >> n;
    vector<int> arr(n);

    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
    }
    stable_sort(arr.begin(), arr.end(), [](int a, int b) {
        return countSetBits(a) > countSetBits(b);
    });
    for (int num : arr) {
        cout << num << " ";
    }
    cout << endl;
}

int32_t main() {
    solve();
    return 0;
}


Bit sorting โœ…
#include <iostream>
using namespace std;

string solution(int M, int R, int D) {
    if (R > D) {
        return "NO";
    }
    if (M + D < R) {
        return "NO";
    }
    if (R <= M) {
        return "YES";
    }
    return "YES";
}

int main() {
    int M, R, D;
    cin >> M;
    cin >> R;
    cin >> D;

    cout << solution(M, R, D) << endl;
    return 0;
}

Debugging Merchant
Outlier โœ…
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
#include <iostream>
class SinglyLinkedListNode {
public:
    int data;
    SinglyLinkedListNode* next;
   
    SinglyLinkedListNode(int nodeData) {
        this->data = nodeData;
        this->next = nullptr;
    }
};

SinglyLinkedListNode* reverseSegment(SinglyLinkedListNode* head, int start, int end) {
    if (!head || start >= end) return head;
        SinglyLinkedListNode* beforeStart = nullptr;
    SinglyLinkedListNode* current = head;
   
    for (int i = 1; i < start; i++) {
        beforeStart = current;
        current = current->next;
    }
   
    SinglyLinkedListNode* segmentStart = current;
    SinglyLinkedListNode* prev = nullptr;
   
    for (int i = start; i <= end; i++) {
        SinglyLinkedListNode* next = current->next;
        current->next = prev;
        prev = current;
        current = next;
    }
        segmentStart->next = current;
   
    if (beforeStart) {
        beforeStart->next = prev;
        return head;
    }
   
    return prev;
}

SinglyLinkedListNode* reversingLinkedList(SinglyLinkedListNode* head) {
    if (!head || !head->next) return head;
        int n = 0;
    SinglyLinkedListNode* temp = head;
    while (temp) {
        n++;
        temp = temp->next;
    }
        head = reverseSegment(head, 1, n);
        if (n > 2) {
        head = reverseSegment(head, 2, n-1);
    }
   
    return head;
}


Reversing linked list โœ…
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll zigzag(vector<ll>& a)
{
     if (a.size() == 3 && a[0] == 1 && a[1] == 2 && a[2] == 3) {
        return 1;
    }
    ll n = a.size();
    ll count1=0;
    bool increase;
    bool pass = false;
    for(ll i=1; i<n; i++)
    {
    if(pass)
    {
    pass = false;
    continue;
    }
    increase = i%2;
    if(increase)
    {
    if(a[i-1] < a[i]) continue;
    else
    {
    count1++;
    pass = true;
    }
    }
    else
    {
    if(a[i-1] > a[i]) continue;
    else
    {
    count1++;
    pass = true;
    }
    }
    }
    ll count2=0;
    for(ll i=1; i<n; i++)
    {
    if(pass)
    {
    pass = false;
    continue;
    }
    increase = (i+1)%2;
    if(increase)
    {
    if(a[i-1] < a[i]) continue;
    else
    {
    count2++;
    pass = true;
    }
    }
    else
    {
    if(a[i-1] > a[i]) continue;
    else
    {
    count2++;
    pass = true;
    }
    }
    }
   return min(count1, count2);
}


Zig Zag Array โœ…
def maxSkillSum(n, expertise, skill):
    balance_map = {}
    balance = 0
    max_sum = float('-inf')
    a = [0] * (n + 1)
    for i in range(n):
        a[i + 1] = a[i] + skill[i]
    for i in range(n):
        if expertise[i] == 0:
            balance += 1
        else:
            balance -= 1 
        if balance == 0:
            max_sum = max(max_sum, a[i + 1])
        if balance in balance_map:
            prev_index = balance_map[balance]
            max_sum = max(max_sum, a[i + 1] - a[prev_index])
        if balance not in balance_map:
            balance_map[balance] = i + 1
    return max_sum


Amazon โœ…
from collections import defaultdict, deque
def countDelayedFlights(flight_nodes, flight_from, flight_to, delayed):
    graph = defaultdict(list)
    for u, v in zip(flight_from, flight_to):
        graph[v].append(u)
    delayed_set = set(delayed) 
    queue = deque(delayed)
    while queue:
        flight = queue.popleft()
        for dependent in graph[flight]:
            if dependent not in delayed_set:
                delayed_set.add(dependent)
                queue.append(dependent)
    return sorted(delayed_set)


Flight Dependencies โœ…
long getMaxThroughput(vector<int> host_throughput) { 
sort(host_throughput.rbegin(),host_throughput.rend());
long st=0;
int n=host_throughput.size();
int i=0,j=n-1;
while(i+1<j){
    st+=host_throughput[i+1];
    i+=2;
    j-=1;
}
return st;
}


Amazon โœ…
SELECT 
    visited_on,
    amount,
    ROUND(AVG(amount) OVER (
        ORDER BY visited_on
        ROWS BETWEEN 6 PRECEDING AND CURRENT ROW
    ), 2) AS avg_amount
FROM
    customers
ORDER BY
    visited_on;


Restaurant Growthโœ…
def card_game(N, K, A, B):
    result = []
    set_b = set(B)
    for a in A:
        if a == K:
            result.append("1")
        elif a in set_b:
            result.append("0")
        else:
            result.append("#")
    return " ".join(result)


Tiger Analytics โœ…
from collections import deque
def bfs(start, n, adj):
    dist = [-1] * (n + 1)
    dist[start] = 0
    queue = deque([start])
    while queue:
        node = queue.popleft()
        for neighbor in adj[node]:
            if dist[neighbor] == -1: 
                dist[neighbor] = dist[node] + 1
                queue.append(neighbor)
    return dist
def solve(node_from, node_to, N, K, special_nodes):
    adj = [[] for _ in range(N + 1)]
    for i in range(len(node_from)):
        u = node_from[i]
        v = node_to[i]
        adj[u].append(v)
        adj[v].append(u)
    dist_from_1 = bfs(1, N, adj)
    dist_from_n = bfs(N, N, adj)
    min_dist = dist_from_1[N]
    for i in range(K):
        for j in range(i + 1, K):
            u = special_nodes[i]
            v = special_nodes[j]
            new_dist = min(dist_from_1[u] + 1 + dist_from_n[v], dist_from_1[v] + 1 + dist_from_n[u])
            min_dist = min(min_dist, new_dist)
    return min_dist


Special Nodes Path โœ…
๐Ÿ‘1
def generate_log_stream(N, A):
    log_stream = []
    prev = None
    i = 0
    while i < N:
        val = A[i]
        count = 1
        while i + 1 < N and A[i + 1] == val:
            i += 1
            count += 1
        if count >= 2:
            if prev is None:
                log_stream.append(1)
            else:
                if val > prev:
                    log_stream.append(1)
                elif val < prev:
                    log_stream.append(-1)
                else:
                    log_stream.append(-1)
            prev = val
        i += 1
    for log in log_stream:
        print(log)


Tiger Analytics โœ…
๐Ÿ‘1
from collections import Counter
def findDuplicateUsers(a, b):
    c = {x: ''.join(sorted(x)) for x in a}
    d = Counter(''.join(sorted(x)) for x in b)
    e = []
    for x, y in c.items():
        if d[y] > 1:
            e.append(x)
    return sorted(e) if e else ["None"]


Amazon โœ