๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
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 <iostream>
#include <vector>
using namespace std;
class FenwickTree {
private:
    vector<long long> tree;
    int n;
   
public:
    FenwickTree(int size) : n(size), tree(size + 1, 0) {}
   
    void update(int index, int value) {
        while (index <= n) {
            tree[index] += value;
            index += index & -index;
        }
    }
   
    long long query(int index) {
        long long sum = 0;
        while (index > 0) {
            sum += tree[index];
            index -= index & -index;
        }
        return sum;
    }
};

int main() {
    int N;
    cin >> N;
   
    vector<int> marks(N + 1);
    FenwickTree fenwickTree(N);
   
    for (int i = 1; i <= N; ++i) {
        cin >> marks[i];
        fenwickTree.update(i, marks[i]);
    }
   
    int Q;
    cin >> Q;
   
    for (int i = 0; i < Q; ++i) {
        int type;
        cin >> type;
       
        if (type == 1) {
            int X, Y;
            cin >> X >> Y;
            int diff = Y - marks[X];
            marks[X] = Y;
            fenwickTree.update(X, diff);
        } else if (type == 2) {
            int K;
            cin >> K;
            cout << fenwickTree.query(K) << endl;
        }
    }
   
    return 0;
}
๐Ÿ‘2
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int helper(vector<vector<int>>& grid, int N, int M) {
vector<vector<pair<int, bool>>> dp(N, vector<pair<int, bool>>(M, {0, false}));
    dp[0][0] = {grid[0][0], (grid[0][0] == 2 || grid[0][0] == -3)};
    for (int i = 0; i < N; ++i) {
    for (int j = 0; j < M; ++j) {
    if (i == 0 && j == 0) continue;
    int max = -1;
    bool has = false;
    if (i > 0) {
    int val = dp[i-1][j].first + grid[i][j];
    bool special = dp[i-1][j].second (grid[i][j] == 2 grid[i][j] == -3);
    if (val > max) {
     max = val;
    has = special;
      }
     }
    if (j > 0) {
    int val = dp[i][j-1].first + grid[i][j];
    bool special = dp[i][j-1].second (grid[i][j] == 2 grid[i][j] == -3);
    if (val > max) {
    max = val;
    has = special;
   }
  }
  dp[i][j] = {max, has};
        }
    }
    int result = dp[N-1][M-1].second ? dp[N-1][M-1].first : 0;
    return result - (dp[N-1][M-1].second ? (grid[N-1][M-1] == 2 || grid[N-1][M-1] == -3 ? grid[N-1][M-1] : 0) : 0);
}

int main() {
    int N, M;
    cin >> N >> M;
    vector<vector<int>> grid(N, vector<int>(M));
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < M; ++j) {
            cin >> grid[i][j];
        }
    }
    int result = helper(grid, N, M);
    cout << result << endl;
    return 0;
}

Space explorerโœ…
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
import java.util.Scanner;

public class Main {
   
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
       
        int N = scanner.nextInt();
       
        int[][] S = new int[N][N];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                S[i][j] = scanner.nextInt();
            }
        }
       

        long[] dp = new long[1 << N];
       
        for (int mask = 0; mask < (1 << N); mask++) {
            for (int i = 0; i < N; i++) {
                if ((mask & (1 << i)) != 0) {
                    for (int j = i + 1; j < N; j++) {
                        if ((mask & (1 << j)) != 0) {
                            dp[mask] += S[i][j];
                        }
                    }
                }
            }
        }
       
       long result = 0;
        for (int mask = 0; mask < (1 << N); mask++) {
            result = Math.max(result, dp[mask]);
        }
       
   }
}

Uber โœ…
#include <bits/stdc++.h>
using namespace std;
#define int long long  

void solve(){
    int n;
    cin>>n;
    vector<int> a(n);
    for(int i=0;i<n;i++) cin>>a[i];
    vector<int> b(n);
    for(int i=0;i<n;i++) cin>>b[i];
    int ans=0;
    int last=b[0];
    int cost=a[0];
    for(int i=1;i<n;i++){
        if(a[i]>=cost)
        {
            last+=b[i];

        }
        else{
            ans+=last*cost;
            last=b[i];
            cost=a[i];
        }
    }
    ans+=last*cost;
    cout<<ans<<endl;

}


int32_t main() {
   int t;
   cin>>t;
   while(t--)
   solve();
   return 0;
}.

/// Mimimum Cost 
sprinklrโœ…
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int maxSupplies(vector<int>& costs, int credits) {
    sort(costs.begin(), costs.end());
    long count = 0;
    for (int cost : costs) {
        if (credits >= cost) {
            credits -= cost;
            count++;
        } else {
            break;
        }
    }
   
    return count;
}.

// SALESFORCE
SAVING THE GALAXYโœ…
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool helper(const vector<int>& prices, int k, int minDiff) {
    int count = 1;
    int last = prices[0];
    for (int i = 1; i < prices.size(); ++i) {
        if (prices[i] - last >= minDiff) {
            count++;
            last = prices[i];
            if (count >= k)
                return true;
        }
    }
   
    return false;
}

int solve(vector<int>& prices, int k) {
    sort(prices.begin(), prices.end());
   
    int left = 1;
    int right = prices.back() - prices.front();
    int result = 0;
   
    while (left <= right) {
        int mid = left + (right - left) / 2;
       
        if (helper(prices, k, mid)) {
            result = mid;
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
   
    return result;


SALESFORCE 
MAXIMUM VALUE AND MINIMUM DIffโœ…
๐Ÿ‘1
import itertools
import sys

def find_distance_time(values):
    for uSpeed, uAlpha, uBeta, uDelta in itertools.permutations(values):
        for distance in range(1, 10**5 + 1):
            time_1 = uAlpha - distance
            time_2 = distance - uDelta
           
            if time_1 > 0 and distance // time_1 == uSpeed and distance * time_1 == uBeta:
                return (distance, time_1)
            if time_2 > 0 and distance // time_2 == uSpeed and distance * time_2 == uBeta:
                return (distance, time_2)
    return -1, -1

def main():
    input = sys.stdin.read
    data = input().split()
   
    t = int(data[0])
    index = 1
    results = []
   
    for _ in range(t):
        test_case = list(map(int, data[index:index+4]))
        index += 4
        distance, time = find_distance_time(test_case)
        results.append(f'{distance} {time}')
   
    for result in results:
        print(result)

if __name__ == "__main__":
    main()


Uber 4 try it
def solution(A, B, C):
    last_two = ["", ""]
    cnt = [A, B, C]
    ans = []
   
    while sum(cnt):
        for ind, letter in enumerate("abc"):
            if last_two[0] == last_two[1] and last_two[0] == letter:
                continue
            if not cnt[ind]:
                continue
            cnt2 = list(cnt)
            cnt2[ind] -= 1
            max_cnt = max(cnt2)
            sum_cnt_not_max = sum(cnt2) - max_cnt
            if max_cnt > sum_cnt_not_max * 2 + 2:
                continue
            last_two[0] = last_two[1]
            last_two[1] = letter
            cnt[ind] -= 1
            ans.append(letter)
            break
   
    return "".join(ans)

Smallest Diverse String โœ…
Salesforce
def minimumCost(data, k):
    data.sort()
    n = len(data)
    a, b = sum(data[::2]), sum(data[1::2])
    v = 0
   
    if k <= a:
        return 0
   
    for i in range(n // 2):
        v += data[~i]
        a, b = b-data[~i],a-data[i]
        if k <= v + a:
            return i + 1
   
    return -1.

// IDLE SERVERโœ…
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
class UnionFind {
public:
    UnionFind(int n) {
        p = vector<int>(n);
        size = vector<int>(n, 1);
        iota(p.begin(), p.end(), 0);
    }

    bool unite(int a, int b) {
        int pa = find(a), pb = find(b);
        if (pa == pb) {
            return false;
        }
        if (size[pa] > size[pb]) {
            p[pb] = pa;
            size[pa] += size[pb];
        } else {
            p[pa] = pb;
            size[pb] += size[pa];
        }
        return true;
    }

    int find(int x) {
        if (p[x] != x) {
            p[x] = find(p[x]);
        }
        return p[x];
    }

    int getSize(int root) {
        return size[root];
    }

private:
    vector<int> p, size;
};

class Solution {
public:
    int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
        int n = graph.size();
        bool s[n];
        memset(s, false, sizeof(s));
        for (int i : initial) {
            s[i] = true;
        }
        UnionFind uf(n);
        for (int i = 0; i < n; ++i) {
            if (!s[i]) {
                for (int j = i + 1; j < n; ++j) {
                    if (graph[i][j] && !s[j]) {
                        uf.unite(i, j);
                    }
                }
            }
        }
        unordered_set<int> g[n];
        int cnt[n];
        memset(cnt, 0, sizeof(cnt));
        for (int i : initial) {
            for (int j = 0; j < n; ++j) {
                if (!s[j] && graph[i][j]) {
                    g[i].insert(uf.find(j));
                }
            }
            for (int root : g[i]) {
                ++cnt[root];
            }
        }
        int ans = 0, mx = -1;
        for (int i : initial) {
            int t = 0;
            for (int root : g[i]) {
                if (cnt[root] == 1) {
                    t += uf.getSize(root);
                }
            }
            if (t > mx || (t == mx && i < ans)) {
                ans = i;
                mx = t;
            }
        }
        return ans;
    }
};

Malware spread โœ…
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
def smallest_lexicographical_string(word, max_operations):
    operations_used = 0
    word = list(word)  # Convert to list for easier manipulation
   
    for i in range(len(word)):
        if operations_used >= max_operations:
            break
       
        current_char = word[i]
       
        if current_char == 'a':
            continue
       
        # Number of operations needed to turn current character into 'a'
        operations_needed = ord(current_char) - ord('a')
       
        if operations_used + operations_needed <= max_operations:
            word[i] = 'a'
            operations_used += operations_needed
        else:
            # Change as much as possible
            new_char = chr(ord(current_char) - (max_operations - operations_used))
            word[i] = new_char
            operations_used = max_operations
            break
   
    return ''.join(word)

Optimal storage โœ…
๐Ÿ‘1