๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
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
๐Ÿš€Vertex Infosoft Solutions Pvt. Ltd is urgently looking for B.tech (CS/IT) candidates with:

Excellent english communication skills ๐Ÿ—ฃ๏ธ for the 'Associate Engineer' position in the Technical Support department.

Interested candidates can share their CVs ๐Ÿ“ƒat
careers@vertexinfosoft.com
def is_valid_expression(s):
    valid_chars = set("0123456789+-*/ ")
    if any(c not in valid_chars for c in s):
        return False
   
    prev_char = ''
    for i, c in enumerate(s.strip()):
        if c in '+-*/':
            if prev_char in '+-*/' or i == 0 or i == len(s.strip()) - 1:
                return False
        prev_char = c
   
    return True

def evaluate_expression(s):
    try:
        result = eval(s)
        if isinstance(result, int):
            return result
        else:
            return -1
    except ZeroDivisionError:
        return -1
    except Exception:
        return -1

def integer_calculator(s):
    s = s.strip()
   
    if not is_valid_expression(s):
        return -1
   
    result = evaluate_expression(s)
   
    return result


Jaguar โœ…
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
#include <bits/stdc++.h>
using namespace std;

struct node {
    int h[26];
    int n;    
}

node null;
vector<node> trie;
void addContact(string &name) {
    int act = 0;
    for (char c : name) {
        int p = c - 'a';
        if (!trie[act].h[p]) {
            trie[act].h[p] = trie.size();
            trie.push_back(null);
        }
        act = trie[act].h[p];
        trie[act].n++;
    }
}

int findPartial(string &prefix) {
    int act = 0;
    for (char c : prefix) {
        int p = c - 'a';
        if (!trie[act].h[p]) return 0;
        act = trie[act].h[p];
    }
    return trie[act].n;
}

int main() {
    int n;
    cin >> n;
    string operation, contact;

    null.n = 0;
    for (int i = 0; i < 26; i++) null.h[i] = 0;
    trie.push_back(null);

    vector<int> results;

    for (int i = 0; i < n; i++) {
        cin >> operation >> contact;
        if (operation == "add") {
            addContact(contact);
        } else if (operation == "find") {
            results.push_back(findPartial(contact));
        }
    }

    for (int res : results) {
        cout << res << "\n";
    }

    return 0;
}


Jaguar โœ…
#include <iostream>
#include <vector>
using namespace std;

vector<int> frequencyOfMaxValue(vector<int>& numbers, vector<int>& queries) {
    int n = numbers.size();
    vector<int> answer(n, -1);  
    int maxvalue = -1;          
    int count = 1;             
    for (int i = n - 1; i >= 0; --i) {
        if (numbers[i] == maxvalue) {
            count++;
        } else if (numbers[i] > maxvalue) {
            maxvalue = numbers[i];
            count = 1;
        }
        answer[i] = count;      
    }

    vector<int> result;
    for (int index : queries) {
        result.push_back(answer[index - 1]); 
    }

    return result;
}
 

Frequency ofMaxValue
Wells fargoโœ…
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
#include <bits/stdc++.h>
using namespace std;
vector<int> getPopularityOrder(vector<vector<int>> song_preferences) {
    int n = song_preferences[0].size();
    vector<vector<int>> boxScore(n, vector<int>(n, 0));
    for (const vector<int>& pref : song_preferences) {
        for (int i = 0; i < pref.size() - 1; i++) {
            for (int j = i + 1; j < pref.size(); j++) {
                boxScore[pref[i]][pref[j]] += 1;
            }
        }
    }

    vector<array<int, 2>> wins(n, {0, 0});
    for (int i = 0; i < n; i++) {
        wins[i][0] = i;
        for (int j = i + 1; j < n; j++) {
            int score = boxScore[i][j] - boxScore[j][i];
            if (score >= 0) {
                wins[i][1]++;
            } else {
                wins[j][1]++;
            }
        }
    }

    sort(wins.begin(), wins.end(), [](const array<int, 2>& a, const array<int, 2>& b) {
        if (a[1] == b[1]) {
            return a[0] < b[0];
        } else {
            return b[1] < a[1];
        }
    });

    vector<int> result;
    for (const auto& win : wins) {
        result.emplace_back(win[0]);
    }

    return result;
}


Songs Popularity
Wells Fargo โœ…
def find_smallest_k(mat, n):
    for k in range(n):
        row_condition = all(mat[k][j] == 0 for j in range(n) if j != k)
        column_condition = all(mat[i][k] == 1 for i in range(n) if i != k)
       
        if row_condition and column_condition:
            return k
           
    return -1
n = int(input())
mat = []
for _ in range(n):
    mat.append(list(map(int, input().split())))
result = find_smallest_k(mat, n)
print(result)


Samsung โœ…
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
class Node:
    def __init__(self, key):
        self.data = key
        self.left = None
        self.right = None
def build_tree(level_order):
    if not level_order or level_order[0] == -1:
        return None
   
    iter_order = iter(level_order)
    root = Node(next(iter_order))
    queue = [root]
   
    while queue:
        current_node = queue.pop(0)
        left_value = next(iter_order, -1)
        right_value = next(iter_order, -1)
       
        if left_value != -1:
            current_node.left = Node(left_value)
            queue.append(current_node.left)
       
        if right_value != -1:
            current_node.right = Node(right_value)
            queue.append(current_node.right)
   
    return root
def largest_bst_subtree(root):
    def post_order(node):
        if not node:
            return (float('inf'), float('-inf'), 0, True)
        left_min, left_max, left_size, left_is_bst = post_order(node.left)
        right_min, right_max, right_size, right_is_bst = post_order(node.right)
        if left_is_bst and right_is_bst and left_max < node.data < right_min:
            total_size = left_size + right_size + 1
            return (min(left_min, node.data), max(right_max, node.data), total_size, True)
        else:
            return (0, 0, max(left_size, right_size), False)
    return post_order(root)[2]
input_list = list(map(int, input().split()))
height = input_list[0]
level_order = input_list[1:]
root = build_tree(level_order)
if not root:
    print(-1)
else:
    result = largest_bst_subtree(root)
    print(result)


BST in binary Tree
Samsung โœ…
def minStrength(matrix):
    if not matrix or not matrix[0]:
        return 0
   
    rows, cols = len(matrix), len(matrix[0])
    dp = [0] * cols
   
    dp[-1] = 1 if matrix[-1][-1] > 0 else -matrix[-1][-1] + 1
   
    for j in range(cols - 2, -1, -1):
        dp[j] = max(dp[j + 1] - matrix[-1][j], 1)
   
    for i in range(rows - 2, -1, -1):
        dp[-1] = max(dp[-1] - matrix[i][-1], 1)
        for j in range(cols - 2, -1, -1):
            min_path = min(dp[j], dp[j + 1])
            dp[j] = max(min_path - matrix[i][j], 1)
   
    return dp[0]


Samsung โœ…
import heapq

def finalIndex(N, A):
    dist = [float('inf')] * (N + 2)
    dist[N] = 1
    dist[N + 1] = 0
    pq = [(1, N)] 

    while pq:
        d, node = heapq.heappop(pq)
       
        if d > dist[node]:
            continue

        if node == 1:
            break
      
        prev = node - 1
        if prev >= 1 and d + 1 < dist[prev]:
            dist[prev] = d + 1
            heapq.heappush(pq, (dist[prev], prev))

        prev = A.index(node) + 1
        if d + 1 < dist[prev]:
            dist[prev] = d + 1
            heapq.heappush(pq, (dist[prev], prev))

    return sum(dist[1:N+1])

Final Index - Samsungโœ…
#include <bits/stdc++.h>
using namespace std;
class UnionFind {
public:
    UnionFind(int n) : parent(n), rank(n, 0) {
        for (int i = 0; i < n; ++i) {
            parent[i] = i;
        }
    }

    int find(int u) {
        if (parent[u] != u) {
            parent[u] = find(parent[u]);
        }
        return parent[u];
    }

    void unionSet(int u, int v) {
        int rootU = find(u);
        int rootV = find(v);
        if (rootU != rootV) {
            if (rank[rootU] > rank[rootV]) {
                parent[rootV] = rootU;
            } else if (rank[rootU] < rank[rootV]) {
                parent[rootU] = rootV;
            } else {
                parent[rootV] = rootU;
                ++rank[rootU];
            }
        }
    }

    bool sameSet(int u, int v) {
        return find(u) == find(v);
    }

private:
    vector<int> parent;
    vector<int> rank;
};

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

    int n, m1, m2;
    cin >> n >> m1 >> m2;

    UnionFind ufAlice(n + 1);
    UnionFind ufBob(n + 1);  

    vector<pair<int, int>> connectionsAlice(m1);
    vector<pair<int, int>> connectionsBob(m2);

    for (int i = 0; i < m1; ++i) {
        int u, v;
        cin >> u >> v;
        connectionsAlice[i] = {u, v};
        ufAlice.unionSet(u, v);
    }

    for (int i = 0; i < m2; ++i) {
        int u, v;
        cin >> u >> v;
        connectionsBob[i] = {u, v};
        ufBob.unionSet(u, v);
    }

    int maxConnections = 0;
    for (int u = 1; u <= n; ++u) {
        for (int v = u + 1; v <= n; ++v) {
            if (!ufAlice.sameSet(u, v) && !ufBob.sameSet(u, v)) {
                maxConnections++;
                ufAlice.unionSet(u, v);
                ufBob.unionSet(u, v);
            }
        }
    }

    cout << maxConnections << endl;
    return 0;
}

```
```  Maximizing network connections in dual regions.โœ