๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
9.6K subscribers
5.59K photos
3 videos
95 files
10.1K 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
#include <bits/stdc++.h>
using namespace std;
string makeLexicographicallySmallestPalindrome(string s) {
    int n = s.length();
    unordered_map<char, int> a;
    string b;
    for (char c : s) {
        a[c]++;
    }
    for (char c = 'a'; c <= 'z'; c++) {
        if (a[c] % 2 != 0) {
            b += c;
        }
    }
    int m = b.length();
    int p = 0;
    for (int i = m - 1; i >= m / 2; i--) {
        char x = b[i];
        char y = b[p];
        a[x]--;
        b[i] = y;
        a[y]++;
        p++;
    }

    string c, d, e;
    for (char c1 = 'a'; c1 <= 'z'; c1++) {
        int f = a[c1];
        if (f != 0) {
            if (f % 2 == 0) {
                c += string(f / 2, c1);
            } else {
                c += string((f - 1) / 2, c1);
                d += c1;
            }
        }
    }
    e = c;
    reverse(e.begin(), e.end());
    return c + d + e;
}
int main() {
    string s;
    cin >> s;
    cout <<makeLexicographicallySmallestPalindrome(s) << endl;
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
string all_happy(int a, int b, vector<int> c) {
    vector<int> d = c;
    sort(d.begin(), d.end());
   
    int e = 0;
    vector<int> f(a + 1, 0);
   
    auto g = [&](int h, int i) {
        for (; h <= a; h += h & -h) {
            f[h] += i;
        }
    };
   
    auto j = [&](int h) {
        int k = 0;
        for (; h > 0; h -= h & -h) {
            k += f[h];
        }
        return k;
    };

    for (int l = 0; l < a; ++l) {
        int m = lower_bound(d.begin(), d.end(), c[l]) - d.begin() + 1;
        e += j(a) - j(m);
        g(m, 1);
    }

    if (e == 0) return "YES";
   
    if (b == 0 || e % b != 0) return "NO";
   
   
    return e / b > 0 ? "YES" : "NO";
}


Make them happy โœ…
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
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 โœ